MATLAB Answers


How do I write a script for a GUI.m file that allows me to press a button, choose a (*.csv) file and then copy this (*.csv) file to the current folder so it can be used for an existing simulation?

さんによって質問されました 2012 年 12 月 13 日
I already have a pushbutton that when pressed allows me to browse for a (*.csv) file. I tried the copyfile function as follows but get an error:
[filename, pathname, filerindex] = uigetfile('*.csv');
[success,message,messaageid] = copyfile(pathname,'A:\',filename);
A:\ is a mapped network drive.
I get the following error: Error using copyfile 'Data1.csv' is an invalid option string.
My code for the pushbutton in the GUI.m file is:
% --- Executes on button press in cmd_getDir.
function cmd_getDir_Callback(hObject, eventdata, handles)%****BROWSE button
[filename, pathname, filerindex] = uigetfile('*.csv'); %Get file
[success,message,messaageid] = copyfile('pathname','A:\',filename);%Need to copy file to current folder
assignin('base','FileCSV',filename); %assign the name FileCSV to the file
set(handles.text_directory,'String', filename); %Put filename in string space
% hObject handle to cmd_getDir (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
I understand from the MATLAB help that:
[SUCCESS,MESSAGE,MESSAGEID] = copyfile(SOURCE,DESTINATION,'f') attempts to copy SOURCE to DESTINATION, as above, even if DESTINATION is read-only. The status of the writable attribute of DESTINATION will be preserved.
SOURCE: 1 x n string, defining the source file or directory.
DESTINATION: 1 x n string, defining destination file or directory. The default is the current directory.
MODE: character scalar defining copy mode.
'f' : force SOURCE to be written to DESTINATION. If omitted, copyfile respects the current writable status of DESTINATION.

  0 件のコメント

サインイン to comment.

1 件の回答

回答者: Login_Name
2012 年 12 月 13 日
編集済み: Login_Name
2012 年 12 月 13 日

You need to make complete pathnames. One way to do it is to concatenate the output from UIGETFILE and also to concatenate the destination directory with the filename.
[filename, pathname, filerindex] = uigetfile('*.csv');
[S,M,MI] = copyfile([pathname,filename],['A:\',filename]);

  1 件のコメント

Thank you. Concatenating worked.

サインイン to comment.

Translated by