Loading a variable from deferent .mat files selected using GUI matlab

how can i load a double from a mat file that i selected using push button in gui matlab , whatever the matfile i be able to extract an load a double from it and use it ex: i have a mat file called 1.mat and another 2.mat
whos -file 1.mat
Name Size Bytes Class Attributes
X1RPM 1x1 8 double
X1_BA_time 122136x1 977088 double
X1_DE_time 122136x1 977088 double
X1_FE_time 122136x1 977088 double
dt=new.X1_DE_time;
2nd mat file 2.mat
whos -file 2.mat
Name Size Bytes Class Attributes
X2RPM 1x1 8 double
X2_BA_time 122136x1 977088 double
X2_DE_time 122136x1 977088 double
X2_FE_time 122136x1 977088 double
dt=new.X2_DE_time;
using GUI now 1st push putton
function Data_Callback(~, ~, handles)
[filename, pathname] = uigetfile({'*.mat'},'File Selector');
set(handles.text5,'String', filename)% showing full path name
handles.data=load([pathname, filename]);
data=filename;
setappdata(0,'data',data);
2nd push putton
function ok_Callback(hObject, eventdata, handles)
new=load(getappdata(0,'data'));
now i have the problem , how can i load de time whatever the mat file selected
dt=new.X1 or X2 or,...._DE_time;

1 Comment

Note that you should use fullfile instead of concatenating pathname and filename. You also need to pass pathname to the 2nd callback, otherwise your code will simply ignore the path that the user selects.
Note that this is a good example of why changing variable names inside .mat files is a bad idea: it makes accessing the data more difficult. It is actually much easier to access data in a loop, if all .mat files contains exactly the same variable names. Then this entire problem simply goes away.
And of course having lots of different variable names is a bad way to write code:

Sign in to comment.

 Accepted Answer

>> new.X1_BA_time = 1:3;
>> new.X1_DE_time = 4:6;
>> new.X1_FE_time = 7:9;
>> C = fieldnames(new);
>> X = cellfun('isempty',regexp(C,'^X\d+_DE_time$'));
>> dt = new.(C{~X})
dt =
4 5 6

2 Comments

Tnk'x a lot Stephen for your help
@BOULKROUNE RAMZI: you are very welcome! Remember to accept my answer if it helped you.

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!