Convert string cells to text scalar format

14 ビュー (過去 30 日間)
KiBaek Jang
KiBaek Jang 2022 年 5 月 14 日
回答済み: Walter Roberson 2022 年 5 月 14 日
[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.

回答 (1 件)

Walter Roberson
Walter Roberson 2022 年 5 月 14 日
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.

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by