Convert string cells to text scalar format

14 Ansichten (letzte 30 Tage)
KiBaek Jang
KiBaek Jang am 14 Mai 2022
Beantwortet: Walter Roberson am 14 Mai 2022
[file,path,indx] = uigetfile('*.*','select a single file at a time' ...
,'MultiSelect','on');
fn=string(file);
fp=string(path);
filenp=append(fp,fn);
fielnpl=length(filenp);
filenpX=cellstr(filenp); %??? I don't know this part
direcvalue = filenpX;
addpath('C:\')
direc=direcvalue;
filename = dir(fullfile(direc,'*.jpeg')) %An error is thrown because filename is not in scalar form.

Antworten (1)

Walter Roberson
Walter Roberson am 14 Mai 2022
filenp=append(fp,fn);
This is incorrect. It assumes that the returned path ended with a directory separator, which is not promised. You should be using fullfile() to join the parts together.
Your direc is copied from direcvalue which is copied from filenpX which is (currently) cellstr of filenp. filenp is created from the uigetfile results, but you have multiselect on. That implies that direc refers to one or more files. You want to append *.jpeg and dir() and you are getting problems because you are asking to dir() multiple items.
The answer to that is simple: dir() is not able to search for information about multiple specifications in a single call. You should be using something like arrayfun() or cellfun() . Or perhaps you should use an image datastore.
Your code has the logical problem that uigetfile() is intended to select files, but you are treating the "files" as-if they are directory names. Perhaps that is deliberate, perhaps you are expecting the user to select directories instead of files. It would be more robust to verify that the names returned are all directories before you use them.

Kategorien

Mehr zu File Operations finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by