How can I save a filename as a string, not as a char?

In my code, the user selects a file. That file name is assigned to "File1" as a char. Is there any way I can assign it as a string variable? I need to manipulate the name of the file after and need it in string form. Thanks.
if true
[File1,path] = uigetfile('*.txt');
if isequal(File1,0)
disp('User selected Cancel');
else
disp(['User selected ', fullfile(path,File1)]);
end
end

Antworten (1)

Alexander Jensen
Alexander Jensen am 6 Sep. 2018
Is this what you're looking for? Or am I misunderstanding your question. And why do you need it as a string to manipulate it?
if true
[File1,path] = uigetfile('*.txt');
File1 = string(File1);
if isequal(File1,0)
disp('User selected Cancel');
else
disp(['User selected ', fullfile(path,File1)]);
end
end

5 Kommentare

You should test for 0 before you string() . Also, path is an important function in MATLAB so you should avoid using that as the name of a variable.
[File1, filepath] = uigetfile('*.txt');
if isnumeric(File1)
disp('User selected Cancel');
else
File1 = string( fullfile(filepath, File1) );
disp(['User selected ', File1]);
end
"string()" does not work. I get this warning "Warning: string is obsolete and will be discontinued. Use char instead.".
I need it as a string to manipulate it because I don't see a way of deleting spaces / specific text from a char. For example, you cant use "erase()" on a char variable.
Which MATLAB release are you using? What shows up for
which -all string
I suspect that you might be using a release before R2016b when string() was added.
You can remove trailing non-significant whitespace with deblank(). You can remove leading and trailing non-significant whitespace with strtrim(). You can remove all whitespace including significant whitespace using
S(isspace(S)) = '';
You can remove blanks specifically using
S(S==' ') = '';
You can replace portions of a string using strrep() or regexprep()
This shows up for "which -all string": "built-in (undocumented)"
I am using R2013B.
The string data type was introduced in R2016b. https://www.mathworks.com/help/matlab/ref/string.html
You will find that you do not have erase() or related functions.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 6 Sep. 2018

Kommentiert:

am 6 Sep. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by