Create a loop with string

2 次查看(过去 30 天)
mikymike89
mikymike89 2018-3-17
编辑: Stephen23 2018-3-19
I would like to create a loop command to read (i) file and, then, have all the curves on the same graph. I can minimize my case in the following example:
% with 1<(i)<100;
load TEXT_(i).txt;
x_(i) = TEXT_(i)(:,1);
y_(i) = TEXT_(i)(:,2);
z_(i) = TEXT_(i)(:,2);
XX_(i)=x_(i)+y_(i);
plot(XX_(i),z_(i));

回答(2 个)

KALYAN ACHARJYA
KALYAN ACHARJYA 2018-3-17
编辑:KALYAN ACHARJYA 2018-3-17
% save all text files in a symmetric manner before doing the operation
% names for example text1,text2,text3...
%Save the folder of images in the current directory
path_directory='Folder_name'; % 'Folder name'
original_files=dir([path_directory '/*.txt']);
for i=1:length(original_files) *% if you want till 100 use "for i=1:100"*
filename=[path_directory '/' original_files(i).name];
load TEXT_(i).txt;
x_(i)=TEXT_(i)(:,1);
y_(i)=TEXT_(i)(:,2);
z_(i)=TEXT_(i)(:,2);
XX(i)=x_(i)+y_(i);
plot(XX_(i),z_(i));
hold on
end
  3 个评论
KALYAN ACHARJYA
KALYAN ACHARJYA 2018-3-18
编辑:KALYAN ACHARJYA 2018-3-18
First, don't do any operation (till load file and check is there any error shows during run), just call one by one. Instead of calling from the desktop, keep the folder in Matlab current directory. try. Its perfectly work to call images in my case. I modified the code for text file.
mikymike89
mikymike89 2018-3-19
编辑:mikymike89 2018-3-19
I misunderstand what I have to insert instead of 'Folder name'. All my TEXT files are in the same folder of the Matlab file. So, Could you fix your code in the easy case of the two file TEXT attached? If I try to keep your original code I receive the error "()-indexing must appear last in an index expression." Moreover, as wrote in the last comment, I would like to insert a command to sum each x_(i).
path_directory='Folder_name';
original_files=dir([path_directory '/*.txt']);
for i=1:2
filename=[path_directory '/' original_files(i).name];
load TEXT_(i).txt;
x_(i)=TEXT_(i)(:,1);
y_(i)=TEXT_(i)(:,2);
z_(i)=TEXT_(i)(:,3);
XX(i)=x_(i)+y_(i);
plot(XX_(i),z_(i));
hold on
end

请先登录,再进行评论。


Stephen23
Stephen23 2018-3-19
编辑:Stephen23 2018-3-19
Using command syntax with load is one of the main bugs in the code: as the MATLAB documentation describes the following text is interpreted as literal strings, not a variable that can be indexed into as you are attempting:
In general you should avoid using command syntax. Also it is recommended to always load into a variable rather than directly into the workspace, although in your case dlmread would probably be a better choice for reading the file. Use fullfile rather than string concatenation. It is often more efficient to load data first, before starting to process (e.g. plot) it. This should get you started (untested):
P = 'Folder_name';
D = dir(fullfile(P,'*.txt'));
C = cell(1,numel(D));
for k = 1:numel(D)
F = fullfile(P,S(k).name);
C{k} = dlmread(F);
end
A = cat(3,C{:}); % this lines assumes all matrices are the same size!
W = A(:,1,:)+A(:,2,:);
Z = A(:,3,:);
plot(W,Z)
If you want the files to be imported in the order of the numbers in their names then you will need to sort them: one possibility is to download and use my FEX submission natsortfiles.

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by