MATLAB Answers

access struct data using 'fileName' given by U = load([filePath fileName])

5 views (last 30 days)
Carolina Brum Medeiros
Carolina Brum Medeiros on 29 Mar 2012
Hi,
thanks in advance.
I load a mat file called, for example, bending0001.mat using:
%------------------
[fileName filePath] = uigetfile('*.mat', 'Select data file', '.');
if filePath==0, error('None selected!'); end
U = load([filePath fileName])
%------------------
I want to access the struct data without giving the file name, but using the U. That means:
  • this way :)
length_ = length(U.Trajectories.Labeled.Data(6,:,:));
  • not this way :(
length_ = length(bending0001.Trajectories.Labeled.Data(6,:,:));
what happens is that the higher struct field has the same name as the file, using this feature, I could open multiple files easier without typing the name at each access
thank you a lot,
Carolina
  2 Comments
Carolina Brum Medeiros
Carolina Brum Medeiros on 5 Apr 2012
I mean, instead of typing the name of the different files to access the struct, like this:
length_ = length(bending0001.Trajectories.Labeled.Data(6,:,:));
I search for an automatic way to load the file name in the space ?? below,
length_ = length(??.Trajectories.Labeled.Data(6,:,:));
This way, I can load various files without editing my script each time I'm loading a different .mat struct file.
Thanks for your answer, I didn't receive the notice that someone answered it. Thanks

Sign in to comment.

Answers (1)

Jan
Jan on 5 Apr 2012
Doesn't the already posted U=load() solve the problem sufficiently?
  1 Comment
Carolina Brum Medeiros
Carolina Brum Medeiros on 5 Apr 2012
Thanks for your answer. And the answer for your question is: no.
note the piece of code below:
[fileName filePath] = uigetfile('*.mat', 'Select data file', '.');
% here I select bending0001.mat, for example
if filePath==0, error('None selected!'); end
load( fullfile(filePath, fileName))
>> Analog_SG_full = fileName.Analog.Data(1,:);
answer:
??? Attempt to reference field of non-structure array.
As I access different field in the struct data a lot of times along the script, I don't want to change the file name on each one of them. Clearly, the product from load() is not compatible for the struct path.
Thanks,
Carolina

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by