Error when writing file names into a txt file ("nonscalar strings are unsupported")
조회 수: 16 (최근 30일)
이전 댓글 표시
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
Dyuman Joshi
2024년 1월 14일
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
채택된 답변
Anjaneyulu Bairi
2024년 1월 14일
편집: Anjaneyulu Bairi
2024년 1월 14일
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
Stephen23
2024년 1월 16일
편집: Stephen23
2024년 1월 16일
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);
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 File Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!