필터 지우기
필터 지우기

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

조회 수: 8 (최근 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
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
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 2024년 1월 16일
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! :-) :-) :-)

댓글을 달려면 로그인하십시오.

채택된 답변

Anjaneyulu Bairi
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);
I hope it helps to resolve your query.
  댓글 수: 2
Sim
Sim 2024년 1월 14일
편집: Sim 2024년 1월 14일
Thanks a lot @Anjaneyulu Bairi, very kind! :-)
Stephen23
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 CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by