Plotting data from a mat file that is a struct

4 ビュー (過去 30 日間)
Nick
Nick 2013 年 12 月 17 日
コメント済み: Image Analyst 2013 年 12 月 18 日
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 日
  3 件のコメント
Image Analyst
Image Analyst 2013 年 12 月 18 日
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.
Sean de Wolski
Sean de Wolski 2013 年 12 月 18 日
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 日
% 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 件のコメント
Sean de Wolski
Sean de Wolski 2013 年 12 月 18 日
So you'll need:
S.filename.times %?
Image Analyst
Image Analyst 2013 年 12 月 18 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by