Error when writing file names into a txt file ("nonscalar strings are unsupported")

15 views (last 30 days)
I have a number of pictures in a folder and I would like to simply get their file names and write them into a txt file:
% input: pictures contained in "myfolder"
% 0013_DSC8315.jpg
% 0020_SOF8150.jpg
% 0068_DSC8440.jpg
% 0077_SOF8212.jpg
% 0089_DSC8481.jpg
% my attempt
a = dir('/.../myfolder');
b = {a(3:end).name}';
b = string(strrep(b, '.jpg', ''))
fid =fopen('/.../pics_names.txt', 'w' );
fwrite(fid, b);
fclose(fid);
However, I get the following output and error message
b =
5×1 string array
"0013_DSC8315"
"0020_SOF8150"
"0068_DSC8440"
"0077_SOF8212"
"0089_DSC8481"
Error using fwrite
Cannot write value: nonscalar strings are unsupported.
Error in untitled (line 5)
fwrite(fid, b);
How to solve this error? :-)
  3 Comments
Dyuman Joshi
Dyuman Joshi on 14 Jan 2024
In addition to what Stephen has mentioned, since you know that the extension of all the files is same and you want to read all the files with the same given extension, why not search accordingly?
Here's a simplfied version of what you want to do -
%Generating and saving some plots for example
for k=1:5
plot(rand(1,10))
ax = gca;
exportgraphics(ax,"Plot_" + randi(1000) + ".jpg")
end
%Check the contest of the current folder
ls
Plot_173.jpg Plot_28.jpg Plot_38.jpg Plot_552.jpg Plot_965.jpg
%Search and get files with .jpg extension
jpgFiles = dir('*.jpg');
%Extract the names
names = {jpgFiles.name}
names = 1×5 cell array
{'Plot_173.jpg'} {'Plot_28.jpg'} {'Plot_38.jpg'} {'Plot_552.jpg'} {'Plot_965.jpg'}
%Get the names without the extension
y = extractBefore(names, '.jpg').'
y = 5×1 cell array
{'Plot_173'} {'Plot_28' } {'Plot_38' } {'Plot_552'} {'Plot_965'}
%Write to a text file via writecell() - Assuming you are working with R2019a
%or a later version
writecell(y, 'pics_names.txt')
%Display the contents of the saved text file
type pics_names.txt
Plot_173 Plot_28 Plot_38 Plot_552 Plot_965
Sim
Sim on 16 Jan 2024
Thanks a lot @Stephen23 and @Dyuman Joshifor your insightful comments and solutions!
I will try to integrate both of your solutions to my stuff, many many thanks! :-) :-) :-)

Sign in to comment.

Accepted Answer

Anjaneyulu Bairi
Anjaneyulu Bairi on 14 Jan 2024
Edited: Anjaneyulu Bairi on 14 Jan 2024
Hi,
I understand that you are trying to write the filenames into a text file using fwrite but getting error on "fwrite". You can visit the article on the "fwrite" error at the end of the answer and instead of using "fwrite", you can use "fprintf" to write the filenames into a text file.
Here is the full code for reference:
a = dir('/.../myfolder');
b = {a(3:end).name}';
b = string(strrep(b, '.jpg', ''))
fid =fopen('/.../pics_names.txt', 'w' );
for i = 1:length(b)
fprintf(fid, '%s\n', b(i));
end
fclose(fid);
I hope it helps to resolve your query.
  2 Comments
Stephen23
Stephen23 on 16 Jan 2024
Edited: Stephen23 on 16 Jan 2024
The code has superfluous type changes and some latent bugs (e.g. assumes but does not check the folder contains only the required image files, assumes but does not check file extensions, assumes but does not check if dot directories are the first two names returned by DIR).
More robust by using the DIR format. Simpler with WRITECELL:
S = dir('/.../myfolder/*.jpg');
C = regexprep({S.name}, '\.jpg$', '', 'ignorecase');
writecell(C(:),'/.../pics_names.txt')
or using FPRINTF:
fid = fopen('/.../pics_names.txt', 'wt');
fprintf(fid, '%s\n', C{:});
fclose(fid);

Sign in to comment.

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!