Ask your own question, for FREE!
Mathematics 7 Online
OpenStudy (raffle_snaffle):

load command matlab

OpenStudy (raffle_snaffle):

@jim_thompson5910

OpenStudy (raffle_snaffle):

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.

OpenStudy (raffle_snaffle):

Let me take a pic of the assignment.

OpenStudy (raffle_snaffle):

OpenStudy (raffle_snaffle):

^There is the assignment.

OpenStudy (raffle_snaffle):

so the 12x31 matrix is located in the workspace. since it's in the workspace does that mean it's already important in matlab?

jimthompson5910 (jim_thompson5910):

I think you mean 31x12 ?

OpenStudy (raffle_snaffle):

OpenStudy (raffle_snaffle):

it says 12x31

jimthompson5910 (jim_thompson5910):

odd, the pdf you posted has 31x12 but your workspace shows 12x31

jimthompson5910 (jim_thompson5910):

did you transpose the matrix somewhere?

OpenStudy (raffle_snaffle):

i just uploaded the file from my instructors spotify. it's a 12x31. He must have made a typo.

OpenStudy (raffle_snaffle):

no I didn't transpose it anywhere.

OpenStudy (raffle_snaffle):

jimthompson5910 (jim_thompson5910):

on your pdf, it also says "check row 31..." but the workspace has that matrix with only 12 rows

jimthompson5910 (jim_thompson5910):

so yeah there has to be a typo in the pdf or a mistake with the xls file

jimthompson5910 (jim_thompson5910):

yeah your xls file is 12x31, so the pdf must have the typo

OpenStudy (raffle_snaffle):

Okay. So how do we use the load command?

OpenStudy (raffle_snaffle):

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?

jimthompson5910 (jim_thompson5910):

I'm guessing that `lab02.m` had the matrix built into it? Can I see the source code you have for that?

jimthompson5910 (jim_thompson5910):

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?

OpenStudy (raffle_snaffle):

I used the import tab. I can post a pic if you like a visual.

jimthompson5910 (jim_thompson5910):

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

OpenStudy (raffle_snaffle):

>> weatherData = load(weatherdata) Error using load Argument must contain a string. >>

jimthompson5910 (jim_thompson5910):

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

jimthompson5910 (jim_thompson5910):

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

OpenStudy (raffle_snaffle):

okay give me a sec

jimthompson5910 (jim_thompson5910):

btw you'll have to make sure that the matlab `.m` file and the `.xls` file are in the same directory

OpenStudy (raffle_snaffle):

yep same file. give me a sec

jimthompson5910 (jim_thompson5910):

oh I see in the pdf it says "transpose the data" so I can see why it went from 31x12 to 12x31

OpenStudy (raffle_snaffle):

but the excel file in the directory given to us is a 12x31. Maybe transpose later?

jimthompson5910 (jim_thompson5910):

wait a minute, before you set up lab02.m, did you make a file called weather_data.MAT ?

OpenStudy (raffle_snaffle):

no

jimthompson5910 (jim_thompson5910):

I'm looking at the prelab part

OpenStudy (raffle_snaffle):

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

jimthompson5910 (jim_thompson5910):

did you do this part `save(’weather_data’, ’weatherData’)`

OpenStudy (raffle_snaffle):

no I didn't. BTW we put this in the script or command window?

jimthompson5910 (jim_thompson5910):

it sounds like it's done in the command window

OpenStudy (raffle_snaffle):

>> save('weather_data','weatherData') Error using save Variable 'weatherData' not found.

jimthompson5910 (jim_thompson5910):

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

jimthompson5910 (jim_thompson5910):

you cleared things out. You'll have to re-import

OpenStudy (raffle_snaffle):

i did clear everything out. let me re-import

OpenStudy (raffle_snaffle):

okay i have a file named MATLAB and it has lab02.m and weather_data.xls saved there

OpenStudy (raffle_snaffle):

I imported the data and it is shown in the worksapce window as a 12x31 matrix

jimthompson5910 (jim_thompson5910):

ok now do `save(’weather_data’, ’weatherData’)`

OpenStudy (raffle_snaffle):

kk

OpenStudy (raffle_snaffle):

>> var1 = save('weather_data','weatherData') Error using save Too many output arguments.

jimthompson5910 (jim_thompson5910):

hmm strange

jimthompson5910 (jim_thompson5910):

weatherData is the name of the variable in your workspace right?

OpenStudy (raffle_snaffle):

no small d it is weatherdata

OpenStudy (raffle_snaffle):

weather_data = xls

OpenStudy (raffle_snaffle):

>> save('weatherData','weatherdata')

OpenStudy (raffle_snaffle):

so when I apply this command it doesn't give me an error.

jimthompson5910 (jim_thompson5910):

ok so it sounds like it was just an issue with upper/lower case letters

OpenStudy (raffle_snaffle):

does does that mean it works?

OpenStudy (raffle_snaffle):

so the save command re-defined the matrix in terms of a variable in this case "weatherData"

OpenStudy (raffle_snaffle):

when I type in "weatherdata" in the command window it outputs the matrix

OpenStudy (raffle_snaffle):

weatherData just gives me an error

OpenStudy (raffle_snaffle):

>> 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

jimthompson5910 (jim_thompson5910):

strange considering how the pdf has an uppercase D

OpenStudy (raffle_snaffle):

could be another typo

jimthompson5910 (jim_thompson5910):

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

OpenStudy (raffle_snaffle):

okay so when I download the xls file I need to change the format to an MAT format?

jimthompson5910 (jim_thompson5910):

no, the xls file stays intact in the folder

OpenStudy (raffle_snaffle):

no wait the weatherData.mat is actually saved in the current file

OpenStudy (raffle_snaffle):

I can see it in under current folder

jimthompson5910 (jim_thompson5910):

what are all the files in the folder you're working with?

OpenStudy (raffle_snaffle):

jimthompson5910 (jim_thompson5910):

ok there should be `weather_data.mat` in there too

OpenStudy (raffle_snaffle):

that is weatherData

jimthompson5910 (jim_thompson5910):

hmm odd and you definitely typed ` save(’weather_data’, ’weatherData’)` and not this `save(’weatherData’,’weather_data’)` right?

OpenStudy (raffle_snaffle):

>> save('weather_data', 'weatherData') Error using save Variable 'weatherData' not found.

jimthompson5910 (jim_thompson5910):

show me your workspace againplease

jimthompson5910 (jim_thompson5910):

something isn't transferring but I'm not sure what

OpenStudy (raffle_snaffle):

hold on my email is being weird

jimthompson5910 (jim_thompson5910):

btw, you can take a screenshot with your computer and you dont' have to use your phone to take pics

OpenStudy (raffle_snaffle):

you mean print screen. mine doesn't work for some reason.

OpenStudy (raffle_snaffle):

I think there is a setting on my laptop... I have not looked into it yet.

jimthompson5910 (jim_thompson5910):

ok for some reason, it's called `weatherdata` instead of `weatherData` try `save('weather_data','weatherdata')`

OpenStudy (raffle_snaffle):

sweet i have weather_data.mat and weatherData.mat in current folder

jimthompson5910 (jim_thompson5910):

ok either delete weatherData.mat or move it to another folder so matlab can't see it anymore keep weather_data.mat

OpenStudy (raffle_snaffle):

okay i deleted it

jimthompson5910 (jim_thompson5910):

ok so we're done with the prelab stuff

jimthompson5910 (jim_thompson5910):

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.

OpenStudy (raffle_snaffle):

S = load(weather_data);

jimthompson5910 (jim_thompson5910):

try `load(weather_data)`, without the S in front, and tell me what pops up into your workspace

jimthompson5910 (jim_thompson5910):

make sure your workspace is cleared first

OpenStudy (raffle_snaffle):

>> load(weather_data) Undefined function or variable 'weather_data'. Did you mean: >> load(weatherdata) Error using load Argument must contain a string.

jimthompson5910 (jim_thompson5910):

oh wait, you need single quotes

jimthompson5910 (jim_thompson5910):

try `load('weather_data')`

OpenStudy (raffle_snaffle):

>> S = load('weather_data') S = weatherdata: [12x31 double]

jimthompson5910 (jim_thompson5910):

"Argument must contain a string" means you must use single quotes

jimthompson5910 (jim_thompson5910):

did you try it without the S in front?

OpenStudy (raffle_snaffle):

>> load('weather_data') >> >> >> >>

OpenStudy (raffle_snaffle):

it doesn't do anything

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!