Filter löschen
Filter löschen

inputdlg string and convert variable to string

17 Ansichten (letzte 30 Tage)
Jonas K
Jonas K am 21 Jun. 2017
Kommentiert: Stephen23 am 22 Jun. 2017
Hello, I'm trying to achieve the following:
I need an input dialog box to open and type in a string. For example rock. Then I want rock to be a string (and not 'rock' with apostrophes) Because then rock can be recognized in the second line of code.
image = inputdlg('Enter file name!')
mask = tga_read_image([image '.mask.tga']);
I also tried to convert the variable image to a string, but that didn't work.
Can anybody help me? Cheers, Jonas.

Akzeptierte Antwort

Stephen23
Stephen23 am 21 Jun. 2017
Bearbeitet: Stephen23 am 21 Jun. 2017
You should start by actually reading the documentation of the functions that you are using. If you had done this then you would have learned that inputdlg's "returned variable answer is a cell array containing one answer per text entry field, starting from the top of the dialog box."
So you can simply access the contents of that cell array, giving you:
C = inputdlg('Enter file name!');
mask = tga_read_image([C{1},'.mask.tga']);
Even better would be to use uigetfile or uiputfile.
  4 Kommentare
Jonas K
Jonas K am 22 Jun. 2017
filename = uigetfile('*.txt');
[pathstr,filename,ext] = fileparts(filename);
newname = sprintf('%s.mask.tga',filename)
tga_read_image(newname);
got it, thank you! i need the reference for multiple file formats with all the same name (img1.txt img1.mask.tga img1.1.tga img1.2.tga and so on)
Stephen23
Stephen23 am 22 Jun. 2017
@Jonas K: good work. And for a final improvement you could also use fullfile to generate the complete filepath:
[filename,pathstr] = uigetfile('*.txt');
[~,filename] = fileparts(filename);
newname = sprintf('%s.mask.tga',filename);
newpath = fullfile(pathstr,newname);
tga_read_image(newpath);
The advantage is that the code will correctly process that file regardless of what folder it is in (i.e. your code and the files you are processing do not need to be all in the same folder).

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Star Strider
Star Strider am 21 Jun. 2017
The single quotes will appear in the output because the value is a character array. It will work if you use cell array indexing to return the character array from the cell:
imgstr = inputdlg('Enter file name! ');
imgname = imgstr{:}
test_output = [imgname '.mask.tga']
imgname =
'rock'
test_output =
'rock.mask.tga'
Also, please do not use ‘image’ as a variable name. It is the name of a built-in MATLAB function that you may need later, and won’t be able to use because you will have ‘overshadowed’ it.
  2 Kommentare
Jonas K
Jonas K am 21 Jun. 2017
thank you, and i will. always learning:)
Star Strider
Star Strider am 21 Jun. 2017
My pleasure!

Melden Sie sich an, um zu kommentieren.


Jonas K
Jonas K am 21 Jun. 2017
I combined both answers now.
img = uigetfile('./psmImages/*.txt');
img = strrep(img,'.txt','')
mask = tga_read_image([img '.mask.tga']);
Thanks everybody.
  1 Kommentar
Stephen23
Stephen23 am 21 Jun. 2017
Bearbeitet: Stephen23 am 21 Jun. 2017
Using strrep to remove the file extension like that is not robust code. Consider this filename:
A.txt-2.txt
A much better way to remove the extension is to use the tool that is designed exactly for that purpose, which I showed you in a comment to my answer. Using the correct tool for a task makes your code clearer, less buggy, easier to understand,... and that is why any tool exists.
Also note that sprintf is preferred to string concatenation for generating filenames.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by