acess struct data without typing file name

2 views (last 30 days)
in
you answered my question offering as option U=load(); but unfortunately this doesn't solve my problem.
by using uigetfile I can select the .mat file that contains my structured data. this is good. I have multiple files named, i.e. bending0001, bending0002, bending0003 and so on
to access the data, I'm doing this, for example:
Analog_SG_full = bending0001.Analog.Data(1,:);
as I have multiple files and multiple fields from which I want to extract data, I don't want to type the name bending000X for each access.
Thanks,
Carolina

Answers (1)

Walter Roberson
Walter Roberson on 14 Apr 2012
for K = 1 : 3
fileName = sprintf('bending%04d.mat', K);
U = load(fileName);
fprintf('\nData for file %d\n', K);
U.Analog.Data(1,:)
end
  5 Comments
Walter Roberson
Walter Roberson on 15 Apr 2012
That shouldn't be the case for load() of .mat files unless the "BENDING0001" is part of what is stored in the file. You can use whos() to see the names of the variables in the file. If the BENDING001 is part of what is stored in the file, then when you create those files you might want to consider using save() with the -struct flag instead of plain save()
Anyhow, the work around is like this:
for K = 1 : 3
rootname = sprintf('bending%04d', K);
fileName = [rootname '.mat'];
U = load(filename);
fields = fieldnames(U);
if length(fields) > 1
clear T
T.(rootname) = U;
U = T;
else
rootname = fields{1};
end
fprintf('\nData for file %d\n', K);
U.(rootname).Analog.Data(1,:)
end
Carolina Brum Medeiros
Carolina Brum Medeiros on 20 Apr 2012
Dear Walter, this really works good!!!
thank you so much!

Sign in to comment.

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!