CSV convert will not work with string file name

2 views (last 30 days)
Hello,
I am writing a GUI to convert parts of .mat files into excel files.
the variables tht are to be converted are stored in a listbox entitled 'listbox2' To clarify where they came from I am trying to put the name of the .mat file on them as a prefix but I cannot seem to get the output files to show up at all.
Worse, this fails to generate any errors. I run the csvwrite command but nothing happens.
Here is the code:
function convert_button_Callback(hObject, eventdata, handles) % hObject handle to convert_button (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
filelocation = get(handles.MAT_file, 'string');
loadfile =['load ''' filelocation ''''];
eval(loadfile);
%evaluate the sting as if it were a command. See notes on %apostrophies for this one. %eval(['varlist = who(''-file'' , ''' filelocation ''');']);
exportlist = get(handles.listbox2,'string')
%we need to change the directory to the chosen output and after we have %re-loaded the .mat file into the workspace convert the files %The evalin evaluates the expression in 'base' or workspace
for a = 1:length(exportlist) finaltag = strcat(filelocation, exportlist{a})
outloc = get(handles.CSV_file, 'string');
cdstring = ['cd(''' outloc ''')'];
eval(cdstring);
csvwrite([finaltag,'.csv'],evalin('base',exportlist{a}))
%Orional code
%csvwrite([exportlist{a},'.csv'],evalin('base',exportlist{a}))
end

Accepted Answer

Walter Roberson
Walter Roberson on 12 Sep 2011
Your code can be considerably simplified. Yes, I know the below code is much longer, but I added a lot of error checking; the heart of the code is small and efficient.
olddir = cd;
outloc = get(handles.CSV_file, 'string');
try
cd(outloc);
catch
error(sprintf('failed to cd to %s', outloc));
end
filelocation= get(handles.MAT_file, 'string');
try
S = load(filelocation);
catch
error(sprintf('failed to load file %s', filelocation));
end
for a = 1:length(exportlist)
v = exportlist{a};
if isfield(S, v)
finaltag = [filelocation, v, '.csv'];
try
csvwrite([finaltag,'.csv'],S.(v));
catch
warning(sprintf('was unable to write variable %s to file %s', v, finaltag));
end
else
warning(sprintf('input file %s does not contain variable %s, csv not written for variable', filelocation, v))
end
end
cd(olddir)

More Answers (1)

Walter Roberson
Walter Roberson on 12 Sep 2011
Please strongly avoid using eval()
Your file can be loaded by using command syntax for the load command:
load(filelocation)
where filelocation is a variable.
  2 Comments
William
William on 12 Sep 2011
for a = 1:length(exportlist)
outloc = get(handles.CSV_file, 'string');
cdstring = ['cd(''' outloc ''')'];
eval(cdstring);
filelocation= get(handles.MAT_file, 'string');
finaltag = strcat(filelocation, exportlist{a})
load(filelocation)
csvwrite([finaltag,'.csv'],evalin('base',exportlist{a}))
end
It still will not give me an error nor will it write to excel.
I am stumped
Walter Roberson
Walter Roberson on 12 Sep 2011
Don't use eval for the cd either!
for a = 1:length(exportlist)
olddir = cd;
outloc = get(handles.CSV_file, 'string');
cd(outloc);
filelocation= get(handles.MAT_file, 'string');
finaltag = strcat(filelocation, exportlist{a});
load(filelocation)
csvwrite([finaltag,'.csv'],evalin('base',exportlist{a}))
cd(olddir)
end
Then you are just faced with the question of why you are loading information in to your *current* workspace, but trying to export data from the base workspace. I would suggest that it would probably make more sense to use the form of load() that uses the output argument:
for a = 1:length(exportlist)
olddir = cd;
outloc = get(handles.CSV_file, 'string');
cd(outloc);
filelocation= get(handles.MAT_file, 'string');
finaltag = strcat(filelocation, exportlist{a});
S = load(filelocation);
V = fieldnames(S);
csvwrite([finaltag,'.csv'],S.(V{1});
cd(olddir)
end
The above code assumes that the file you are loading from has only a single variable in it.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!