Convert string cells to text scalar format

17 views (last 30 days)
[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.

Answers (1)

Walter Roberson
Walter Roberson on 14 May 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.

Categories

Find more on File Operations in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!