Inputs arguments as a strings

4 views (last 30 days)
Kondiik
Kondiik on 28 Feb 2014
Every time I type in Commnad Window
PS5_4(mydata.txt, mydata2.txt)
MATLAB displays error:
>> PS5_4(mydata.txt, mydata2.txt)
Undefined variable "mydata" or function "mydata.txt".
I tried several different ways but nothing seems to work. i dont want to use ' ' in the command. Is there anyway I can convert to string?
FUNCTION:
function PS5_4(x, y)
fPath = x;
if isequal(exist(fPath,'file'),2)% check if file name typed by user to read exist
display('Program will perform some operation please wait...') % if exist display message on the screen
display('...')
display('...')
display('...')
data = dlmread(x) % read file name typed by user
data_tp = data'; % transpose matrix
disp('Transposed matrix = ')
disp(' ')
disp(data_tp)
dlmwrite(y, data_tp, ' ') %save with file name typed by user
else % if file name typed by user does not exist display message
warningMessage = sprintf('Warning: file %s does not exist. Please try to type different file name using format ''filename.txt''\n\nProgram will check if file mydata.txt exist if not default matrix will be created:\n1 2 3 4\n5 6 7 8\n9 10 11 12', x);
uiwait(msgbox(warningMessage));
% because file name typed by user did not existed check if default file
% name exist...
if isequal(exist('mydata.txt','file'),2)
disp('file mydata.txt exist')
else % if does not exist...
disp('File mydata.txt does not exist, Program will create default matrix and save it')
D = [1 2 3 4;5 6 7 8;9 10 11 12]; %create a matrix
disp('Default matrix created = ')
disp(' ')
disp(D)
dlmwrite('mydata.txt', D, ' ') % save it to default filename...
D = D'; % flip matrix and
disp('Transposed matrix = ')
disp(' ')
disp(D)
dlmwrite(y, D, ' ') % save to file name typed by user
end
% Default command:
% PS5_4(mydata.txt, mydata2.txt)
end

Answers (0)

Community Treasure Hunt

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

Start Hunting!