load command matlab
@jim_thompson5910
so i have some data in an excel file. I had imported it into matlab using the tab that says import, however I want to learn how to import the file using the load command.
Let me take a pic of the assignment.
^There is the assignment.
so the 12x31 matrix is located in the workspace. since it's in the workspace does that mean it's already important in matlab?
I think you mean 31x12 ?
it says 12x31
odd, the pdf you posted has 31x12 but your workspace shows 12x31
did you transpose the matrix somewhere?
i just uploaded the file from my instructors spotify. it's a 12x31. He must have made a typo.
no I didn't transpose it anywhere.
on your pdf, it also says "check row 31..." but the workspace has that matrix with only 12 rows
so yeah there has to be a typo in the pdf or a mistake with the xls file
yeah your xls file is 12x31, so the pdf must have the typo
Okay. So how do we use the load command?
weatherData = load(weatherdata) put this in my script and run? Since the 12x31 matrix is in the workspace does that mean it's already uploaded?
I'm guessing that `lab02.m` had the matrix built into it? Can I see the source code you have for that?
wait, nvm, it says to make a new file. So you'd have to import the data in. The question is, how did you import it? Did you use another tool? Or was it already done for you?
I used the import tab. I can post a pic if you like a visual.
no that's fine. I know what you're talking about. So what you should do is clear everything out and try out the bit of code `weatherData = load(weatherdata)` to see if it works or not
>> weatherData = load(weatherdata) Error using load Argument must contain a string. >>
ok so I'm reading here http://www.mathworks.com/help/matlab/import_export/supported-file-formats.html that the `load` command only works for MAT files (or ASCII text files but that's a different article). To get XLS files to work, you'll need to use the `xlsread` command http://www.mathworks.com/help/matlab/ref/xlsread.html
so it would look like this A = xlsread(filename) replace A with the matrix you want to call it replace filename with the string of the file's name, make sure to use single quotes example: weatherData = xlsread('weatherdata.xls'); the semicolon is used to suppress output to the command window
okay give me a sec
btw you'll have to make sure that the matlab `.m` file and the `.xls` file are in the same directory
yep same file. give me a sec
oh I see in the pdf it says "transpose the data" so I can see why it went from 31x12 to 12x31
but the excel file in the directory given to us is a 12x31. Maybe transpose later?
wait a minute, before you set up lab02.m, did you make a file called weather_data.MAT ?
no
I'm looking at the prelab part
i downloaded the excel then i imported it into matlab. the excel file is called weather_data and the 12x31 matrix in the workspace is called weatherdaya
did you do this part `save(’weather_data’, ’weatherData’)`
no I didn't. BTW we put this in the script or command window?
it sounds like it's done in the command window
>> save('weather_data','weatherData') Error using save Variable 'weatherData' not found.
so if I'm reading the prelab instructions right, I think you're supposed to step 1) import the xls file into matlab's workspace step 2) save the data to a file (like a MAT file) that can be used by the `load` command later
you cleared things out. You'll have to re-import
i did clear everything out. let me re-import
okay i have a file named MATLAB and it has lab02.m and weather_data.xls saved there
I imported the data and it is shown in the worksapce window as a 12x31 matrix
ok now do `save(’weather_data’, ’weatherData’)`
kk
>> var1 = save('weather_data','weatherData') Error using save Too many output arguments.
hmm strange
weatherData is the name of the variable in your workspace right?
no small d it is weatherdata
weather_data = xls
>> save('weatherData','weatherdata')
so when I apply this command it doesn't give me an error.
ok so it sounds like it was just an issue with upper/lower case letters
does does that mean it works?
so the save command re-defined the matrix in terms of a variable in this case "weatherData"
when I type in "weatherdata" in the command window it outputs the matrix
weatherData just gives me an error
>> weatherData Undefined function or variable 'weatherData'. Did you mean: >> weatherdata weatherdata = Columns 1 through 13 0 0 272 0 0 0 0 9 26 1 0 0 0 61 103 0 2 0 0 0 2 0 68 3 3 1 2 0 17 27 0 10 0 0 30 25 0 0 0 260 1 0 0 0 1 3 0 5 0 0 0 0 47 0 0 0 5 115 49 81 0 0 0 0 1 0 0 30 42 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0 0 58 157 52 0 45 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 14 156 0 0 0 0 110 130 0 14 1 163 5 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 45 0 0 0 15 37 0 36 Columns 14 through 26 50 85 0 0 90 9 0 0 1 15 224 0 0 0 0 0 1 135 3 66 0 0 0 5 0 0 45 70 0 0 0 0 0 27 26 0 0 9 6 0 10 51 0 0 0 0 0 0 0 0 0 9 10 1 0 0 0 10 0 0 0 0 0 0 0 0 0 97 133 0 0 0 0 0 2 2 240 16 0 0 0 0 0 7 0 21 0 0 0 10 0 6 0 0 0 0 0 0 1 0 0 158 62 1 0 0 0 0 0 0 0 0 37 0 0 0 0 19 0 0 0 0 0 30 92 0 0 0 0 0 0 0 0 0 0 0 1 26 0 2 2 10 255 97 0 0 0 0 0 20 2 34 0 0 0 0 Columns 27 through 31 0 0 0 33 33 0 62 0 0 0 78 0 5 8 0 35 13 86 0 0 2 0 0 0 0 35 14 14 8 0 0 5 0 0 0 0 0 0 0 0 6 138 58 10 0 0 0 0 0 1 2 0 0 0 0 0 0 0 0 0
strange considering how the pdf has an uppercase D
could be another typo
ok so far in the prelab set up we've imported the xls file into matlab's workspace then you use the save command to save the data into a MAT file which matlab can read/write to then when it comes to the lab02.m file, you'll rely on the MAT file to import the data (not the xls file). Remember the `load` command doesn't work with xls files but it works with MAT files
okay so when I download the xls file I need to change the format to an MAT format?
no, the xls file stays intact in the folder
no wait the weatherData.mat is actually saved in the current file
I can see it in under current folder
what are all the files in the folder you're working with?
ok there should be `weather_data.mat` in there too
that is weatherData
hmm odd and you definitely typed ` save(’weather_data’, ’weatherData’)` and not this `save(’weatherData’,’weather_data’)` right?
>> save('weather_data', 'weatherData') Error using save Variable 'weatherData' not found.
show me your workspace againplease
something isn't transferring but I'm not sure what
hold on my email is being weird
btw, you can take a screenshot with your computer and you dont' have to use your phone to take pics
you mean print screen. mine doesn't work for some reason.
I think there is a setting on my laptop... I have not looked into it yet.
ok for some reason, it's called `weatherdata` instead of `weatherData` try `save('weather_data','weatherdata')`
sweet i have weather_data.mat and weatherData.mat in current folder
ok either delete weatherData.mat or move it to another folder so matlab can't see it anymore keep weather_data.mat
okay i deleted it
ok so we're done with the prelab stuff
in your lab02.m file, make sure you have the load command set up so you can import the data from the MAT file into the workspace keep in mind every time you run this m file, it will clear the command line (clc command) and clear the workspace (clear command). So this is why the load command is needed.
S = load(weather_data);
try `load(weather_data)`, without the S in front, and tell me what pops up into your workspace
make sure your workspace is cleared first
>> load(weather_data) Undefined function or variable 'weather_data'. Did you mean: >> load(weatherdata) Error using load Argument must contain a string.
oh wait, you need single quotes
try `load('weather_data')`
>> S = load('weather_data') S = weatherdata: [12x31 double]
"Argument must contain a string" means you must use single quotes
did you try it without the S in front?
>> load('weather_data') >> >> >> >>
it doesn't do anything
Join our real-time social learning platform and learn together with your friends!