how to read data from a .mat file

Hi: I have a .mat file named Ey1a which I need to use it in my code as one of the inputs. The file contains complex data (real and imaginary).I read the file like this:
dlmread Ey1a.mat
but I had this error:
Mismatch between file and format string.
Trouble reading number from file (row 1, field 1) ==> MATLA
Error in ==> fiber_commentedoriginal at 38 dlmread Ey1a.mat;
also, I need to use the conjugate of Ey1a in my code, so how to do this as I can not load the Ey1a.mat file itself

 Risposta accettata

Try
S = load('Ey1a.mat');
and see help for load

22 Commenti

This worked, but it gave another error. It seems to disagree with the plot command "imagesc" which I used to plot real (S). Here is the error:
Undefined function or method 'real' for input arguments of type 'struct'.
Error in ==> fiber_commentedoriginal at 49
figure(1);imagesc(real(S));title('field')
per isakson
per isakson il 1 Apr 2015
Modificato: per isakson il 1 Apr 2015
S is a structure. It has one field for each variable in the mat-file. I repeat, see the help on load.
What does
>> S
show?
Naema
Naema il 1 Apr 2015
doing >> S in the command window gives the error: unexpected Matlab operator. Will try to see the help on load. I wish load reads all the real and imaginary numbers as this is what I seek
oh, sorry. This is what I get: S =
Ey1a: [3001x3001 double]
It does! Type S and hit enter. ">>" is the Matlab prompt on my system.
Naema
Naema il 1 Apr 2015
Per isakson, please,how to get the conjugate of S. I need to use it in my code.
We misunderstand each other; a shared desktop would help:-)
Run this
Z = struct('name1',1+1i, 'name2', 2-2i );
Z
it displays
Z =
name1: 1.0000 + 1.0000i
name2: 2.0000 - 2.0000i
What does
S
display?
Naema
Naema il 1 Apr 2015
Modificato: Naema il 1 Apr 2015
S displays this :
Ey1a: [3001x3001 double]
I answered this in the previous comments
per isakson
per isakson il 1 Apr 2015
Modificato: per isakson il 1 Apr 2015
Missed that. Try
Z = conj( S.Eyla1 );
S is kind of a container for all variables in the mat-file.
do you mean:
Z= conj(S.Ey1a); %note Ey1a not Ey1a1
I got this error:
??? Reference to non-existent field 'Eyla'.
per isakson
per isakson il 1 Apr 2015
Modificato: per isakson il 1 Apr 2015
I think you used lower case "L" instead of "one".
The third character is that a lower case "L" or a one?
>> double('Ey1a')
ans =
69 121 49 97
>> double('Eyla')
ans =
69 121 108 97
Never use lower case "L" in variable names!
Naema
Naema il 1 Apr 2015
Modificato: Naema il 1 Apr 2015
The third character is one and the answer I got is the first numbers row
Naema
Naema il 1 Apr 2015
yes, you are right. I wrote it again and got a new matrix Z of the size 3001x3001. Should I consider Z now as the conjugate of S?
Yes, but it's a lower case "L" in
??? Reference to non-existent field 'Eyla'.
Naema
Naema il 1 Apr 2015
yes, I just wrote lower case "L" in command window by mistake. My variable has the number "one". I just reviewed my previous command which produced the error and found that I wrote lower case "L" but my variable does not have this letter, it only has the number 1 (Ey1a)
per isakson
per isakson il 1 Apr 2015
Modificato: per isakson il 1 Apr 2015
Retry
Z = conj( S.Ey1a );
Naema
Naema il 1 Apr 2015
Modificato: Naema il 1 Apr 2015
It worked, I see a variable named Z was saved in the work space. Its size is 3001x3001
I used Z in my code as the conjugate, but I had another problem. See below: ??? Undefined function or method 'times' for input arguments of type 'struct'.
Error in ==> fiber_commentedoriginal at 98
Norma = sum(sum(Ey2a4.*conj(Ey2a4)))*sum(sum(S.*Z));
Fine!
Naema
Naema il 1 Apr 2015
Yes, but why can't I multiply S with Z? The original data must be multiplied with the conjugate.
Stephen23
Stephen23 il 1 Apr 2015
Modificato: Stephen23 il 1 Apr 2015
@Naema: when you load the .MAT file using load:
S = load(...);
it puts all of the data (that is in the .MAT file) into a structure named S. This is what the documentation clearly states.
The structure S is not your original data array, but a container that contains your data array. A structure is like a box: and just like a box can contain something, so can a structure hold your data. When you wish to access your data, you first need to get it out of the structure, just like you would get something out of the box before you can use it. To access data in a structure we use this notation:
S.variable
where in your case the variable is the name Ey1a or something similar. So this should work for accessing the data in your structure:
S.Ey1a
If you try to use S for your calculation, then this is still just the structure and not your data that is inside the structure, thus the error. You need to get your data out of the structure before you can use it.
You can also have a look inside a structure in your workspace (double click to open it up), or use the command fieldnames(S) to get a list of all of the variables that are contained in the structure.
Naema
Naema il 1 Apr 2015
Stephen: Yes, it works now. Before I multiply Z and S . I had to access both of them and then multiply them together.

Accedi per commentare.

Più risposte (4)

James Tursa
James Tursa il 1 Apr 2015
Modificato: James Tursa il 1 Apr 2015
Use the load function to read .mat files. E.g.,
load Ey1a.mat

4 Commenti

Naema
Naema il 1 Apr 2015
thanks. I used load but did not know how to get the conjugate of the loaded file. I wrote: S= load('Ey1a.mat'); how to get the conjugate of S?
per isakson
per isakson il 1 Apr 2015
Modificato: per isakson il 1 Apr 2015
What does
whos -file Ey1a.mat
show?
It shows this:
Name Size Bytes Class Attributes
Ey1a 3001x3001 144096016 double complex
LUI PAUL
LUI PAUL il 1 Apr 2015
Modificato: LUI PAUL il 1 Apr 2015
load('Eyla.mat');
Eyla=conj(Eyla);

Accedi per commentare.

Vinod Sudheesh
Vinod Sudheesh il 1 Apr 2015

1 voto

You could use the "load" command to read data stored in a MAT file.
>> load Ey1a.mat
The "dlmread" function is used for reading ASCII delimited file.
LUI PAUL
LUI PAUL il 1 Apr 2015

0 voti

for other directory.....
load('D:\data_file\Eyla.mat')
for current directory load('Eyla.mat');
or follow the figure....
LUI PAUL
LUI PAUL il 1 Apr 2015

0 voti

to get conjugate
load('Eyla.mat');
Eyla=conj(Eyla);

Richiesto:

il 1 Apr 2015

Commentato:

il 1 Apr 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by