Inputs arguments as a strings

5 次查看(过去 30 天)
Kondiik
Kondiik 2014-2-28
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

回答(1 个)

Vidhi Agarwal
Vidhi Agarwal 2024-9-17
The error you're encountering is due to MATLAB interpreting “mydata.txt” and “mydata2.txt” as variables rather than string literals. In MATLAB, to pass file names as arguments to a function, you must use string literals or character arrays. To resolve this issue you can try below given methods:
You can wrap your file names with the “string function, which is available in MATLAB R2016b and later. This allows you to use double quotes for strings, you can directly use double quotes without needing the string function.
And update the function for the same. Below is the updated code for the same:
function PS5_4(x, y)
% Convert string objects to character arrays if necessary
if isstring(x)
x = char(x);
end
if isstring(y)
y = char(y);
end
% Check if the file exists
if isequal(exist(x, 'file'), 2)
disp('Program will perform some operation, please wait...')
disp('...')
data = dlmread(x); % Read data from the file
data_tp = data'; % Transpose the matrix
disp('Transposed matrix = ')
disp(' ')
disp(data_tp)
dlmwrite(y, data_tp, ' '); % Save transposed matrix to file
else
warningMessage = sprintf('Warning: file %s does not exist. Please try to type a different file name using format ''filename.txt''\n\nProgram will check if file mydata.txt exists; if not, a default matrix will be created:\n1 2 3 4\n5 6 7 8\n9 10 11 12', x);
uiwait(msgbox(warningMessage));
% Check if the default file exists
if isequal(exist('mydata.txt', 'file'), 2)
disp('File mydata.txt exists')
else
disp('File mydata.txt does not exist, Program will create a default matrix and save it')
D = [1 2 3 4; 5 6 7 8; 9 10 11 12]; % Create a default matrix
disp('Default matrix created = ')
disp(' ')
disp(D)
dlmwrite('mydata.txt', D, ' '); % Save the default matrix
D = D'; % Transpose the matrix
disp('Transposed matrix = ')
disp(' ')
disp(D)
dlmwrite(y, D, ' '); % Save transposed matrix with user-specified name
end
end
end
Output for the updated code is given below:
Hope that Helps!

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by