Error when writing file names into a txt file ("nonscalar strings are unsupported")
15 views (last 30 days)
Show older comments
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
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
%Search and get files with .jpg extension
jpgFiles = dir('*.jpg');
%Extract the names
names = {jpgFiles.name}
%Get the names without the extension
y = extractBefore(names, '.jpg').'
%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
Accepted Answer
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);
Atricle on fwrite error : https://in.mathworks.com/matlabcentral/answers/92726-why-do-i-receive-cannot-write-value-unsupported-class-struct-error-when-writing-a-structure-using
- You can also visit the below MathWorks documentaion link for more information on "fwrite" and "fprintf" : https://in.mathworks.com/help/matlab/ref/fwrite.html
- https://in.mathworks.com/help/matlab/ref/fprintf.html
I hope it helps to resolve your query.
2 Comments
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);
More Answers (0)
See Also
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!