Effacer les filtres
Effacer les filtres

Create a loop with string

2 vues (au cours des 30 derniers jours)
mikymike89
mikymike89 le 17 Mar 2018
Modifié(e) : Stephen23 le 19 Mar 2018
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));

Réponses (2)

KALYAN ACHARJYA
KALYAN ACHARJYA le 17 Mar 2018
Modifié(e) : KALYAN ACHARJYA le 17 Mar 2018
% 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 commentaires
KALYAN ACHARJYA
KALYAN ACHARJYA le 18 Mar 2018
Modifié(e) : KALYAN ACHARJYA le 18 Mar 2018
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 le 19 Mar 2018
Modifié(e) : mikymike89 le 19 Mar 2018
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

Connectez-vous pour commenter.


Stephen23
Stephen23 le 19 Mar 2018
Modifié(e) : Stephen23 le 19 Mar 2018
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.

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by