If you want to allow the user to choose the file that you are to open, then you could use uigetfile which will return the selected filename (and not try to automatically load the file like uiopen). You can then "look into" the file to see what is there. Let's assume that you only allow the user to open files with a mat extension. Then
[filename,pathname,filterindex] = uigetfile('*.mat');
if ischar(filename) && filterindex==1
end
The above code allows us to get the user to choose a file that matches our extension (mat). We ignore the case where the user cancels the dialog OR the user selects another filter (All Files, say) by using the appropriate conditions.
Now that we have a file, we can "look into" it with whos. In the body of the above if statement, we do the following
datInMat = whos('-file',fullfile(pathname,filename));
Now datInMat will be an array of one or more structures for each variable that is in the mat file. For example, if a mat file has a single variable T (table) then
datInMat =
name: 'T'
size: []
bytes: 2010
class: 'table'
global: 0
sparse: 0
complex: 0
nesting: [1x1 struct]
persistent: 0
Given the class attribute, you can then decide what to do with the data - ignore it, or load it
if strcmpi(datInMat(1).class,'table')
varStruct = load(fullfile(pathname,filename),datInMat(1).name);
handles.file = varStruct.(datInMat(1).name);
end
In the above, since the first variable is a table, we load it into the varStruct variable (a structure). But we don't want the structure, just the field that corresponds to the table. So we use the field name (which is the variable name of the table) to access that table from the varStruct and copy it to the handles.file.
It may be useful to put a breakpoint somewhere in the above code and step through and observe what is happening.
The above can now be expanded to handle multiple variables in a mat file, variables of different type/classes (cell, double, etc.), and whether the variables are scalars, arrays, or matrices (using the size field).
Though since this uses load, this may cause a problem given your statement When I used load, it read my Datestr as a number and only gave me the first digit.
Try the above and see what happens!
Best Answer