Unable to save an Excel file as a csv type file to specific subfolder

2 views (last 30 days)
using Matlab2012a and I am trying to open an excel file, convert to csv and then save the csv file with a new name to a specific subfolder. I am able to open the excel file, convert to csv type and rename...but unable to save the csv file to the subfolder of my choosing. MATLAB defaults saving the file to the highest level folder in the path which is a folder named 'BCIS'. I want to save it to the subfolder 'subFolder1a'
The code I'm using is as follows: ...
% reading excel and convert to cell array
[~,readfile,~] = xlsread([FilePath FileName]);
Tfile = readfile(2:end,2);
[nrows,~]= size(Tfile);
% create dialogue box for user entered file name to save
dlg_title = 'Save Txt File';
num_lines = 1;
def = {''};
answer = inputdlg('Please enter a name to save file:',dlg_title,num_lines,def);
% Convert file to csv
filename = (answer{1,1});
fid = fopen(filename, 'w');
for row=1:nrows
fprintf(fid,'%s', Tfile{row,:})
end
fclose(fid)
% save file to subfolder
FilePathText = 'C:\BCIS\subFolder\subFolder1\SubFolder1a\';
filename = uiputfile({'*.csv','Data Files (*.csv)'},...
'Save Ticker List As',...
[FilePathText filename]);
Any help greatly appreciated.

Accepted Answer

dpb
dpb on 28 Dec 2013
uiputfile doesn't do any saving at all; it just returns the filename selected by the user for the subsequent file operation. You need to follow up with a call to csvwrite or similar to actually save the file.
ASIDE TMW:
This is the second time I've seen the identical question/misunderstanding in the last couple of weeks. Looks like documentation should be more clear on this point.

More Answers (1)

Image Analyst
Image Analyst on 28 Dec 2013
You need to get the folder from uiputfile() then call fullfile() to combine them and finally call csvwrite:
[baseFileName, folder] = uiputfile(.............whatever....
fullFileName = fullfile(folder, baseFileName);
csvwrite(fullFileName, yourMatrix);
Then if you're going to do that you don't need all the prior stuff about using fprintf() to write it since csvwrite() will do that anyway.

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!