Plotting data from a mat file that is a struct

I am challenged by structures and cells, not being a programmer by design. I have a mat struct, something like filename.mat with various arrays. Let's say there is one array called counts and another called times, with a single column of numbers each. How does one plot one array against another? Specifically how do you pick up the array you want out of several arrays in that mat file? Apologies if that is too simple a question for some of you.

답변 (2개)

Sean de Wolski
Sean de Wolski 2013년 12월 17일

1 개 추천

S = load('filename.mat');
plot(S.times,S.counts);
Doc:

댓글 수: 3

Nick
Nick 2013년 12월 17일
편집: Walter Roberson 2013년 12월 18일
Thanks for the prompt reply. First line is fine, second line gives an error
Reference to non-existent field 'times'.
Error in tryplot (line 2)
plot(S.times,S.counts);
In fact any field I try from that struct gives the same error. Also times(:,1) in that expression gives the same error. Typing S.times gives the same error (Reference to non-existent field 'times'). Clicking on the field, however, displays the contents fine. Matlab reports the mat file is a struct.
>> whos
Name Size Bytes Class Attributes
S 1x1 3570 struct
Is this a paradox?
Edit: S.filename.times works, plot(S.filename.times,S.filename.counts); also works. I am mystified but I can at least plow on.
You didn't store counts and times separately. You stored filename, and filename was a structure that you created with members/fields called times and counts. See my answer on how to use fieldnames to check on whether or not fields exists and which ones actually do exist.
Nick, I don't know how you created the MAT file but if you created it by saving two variables times and counts, what I have will work. Here is a full example:
times = 1:10;
counts = randperm(10);
save('filename.mat','times','counts');
clear
S = load('filename.mat');
plot(S.times,S.counts);

댓글을 달려면 로그인하십시오.

Image Analyst
Image Analyst 2013년 12월 18일

0 개 추천

% Read in structure from mat file.
storedStructure = load('filename.mat');
% For fun, let's see what fields this structure has.
fieldNames = fieldnames(storedStructure) % No semicolon
% You should see counts and times listed in the command window
% if they were actually stored there.
% Tell user whether or not counts is there.
hasField = isfield(storedStructure, 'counts');
if hasField
uiwait(helpdlg('Your structure has a field called counts'));
else
uiwait(helpdlg('Your structure DOES NOT HAVE a field called counts'));
end
% Tell user whether or not times is there.
hasField = isfield(storedStructure, 'times');
if hasField
uiwait(helpdlg('Your structure has a field called times'));
else
uiwait(helpdlg('Your structure DOES NOT HAVE a field called times'));
end
Tell me what you observe in the command window and in the message boxes.

댓글 수: 4

See the FAQ http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F for a good description of cell arrays.
Nick
Nick 2013년 12월 18일
편집: Nick 2013년 12월 18일
Many thanks for your feedback and the helpful code. The answer to your code is that the fields do not exist. However, fieldNames = filename, so the file has a single field with the name of the file (is that not confusing), which field in turn contains the fields we are talking about.
Sean, your code of course works and is the correct answer to my question, although it did not solve my problem. That mat file was produced by commercially available software exporting data into a matlab readable file. Sadly, you do not get to work straight away with simple examples in real life. Thanks to both of you for your feedback.
So you'll need:
S.filename.times %?
That's what I would think Sean. I agree with you Nick that using filename as the name of a structure that contains fields that have nothing to do with a filename is very confusing. Personally I would bring it up to the publisher and let them know.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Structures에 대해 자세히 알아보기

태그

질문:

2013년 12월 17일

댓글:

2013년 12월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by