How to set the name with num2str in for loop?
조회 수: 4 (최근 30일)
이전 댓글 표시
Hi!
I run the c code and have a number of .dat file.
% cp_1.dat, cp_2.dat and so on.
I want to import and make some results using them.
But I'm not able to use it.
My code is below:
for k = 1:1000
load(['cp_',num2str(k),'.dat']);
A = ['cp_',num2str(k)]; %%%%%%%%%%%% The problem!!!
if k==1
B = zeros(size(A));
end
% This loop shifts the array.
for i = 1:7
ii = abs(i-8);
A(i+14,:) = A(ii,:);
A(i+21,:) = A(ii,:);
end
A(length(a)+1,:) = A(1,:);
B(:,:) = B(:,:)+A(:,:)
end
I hope you understand my aim.
Best,
Sopo
댓글 수: 3
Stephen23
2022년 10월 22일
"When I use a command 'readmatrix' instead of 'load', I get a error"
Because you did not fill out the Release field it is assumed that you have a recent release. But apparently you do not.
Try using DLMREAD or CSVREAD or whatever your installed MATLAB version supports.
채택된 답변
Bjorn Gustavsson
2022년 10월 21일
Perhaps it is as simple as:
for k = 1:1000
% assign the content of the file directly to variable A:
A = load(['cp_',num2str(k),'.dat']);
if k==1
B = zeros(size(A));
end
% This loop shifts the array.
for i = 1:7
ii = abs(i-8);
A(i+14,:) = A(ii,:);
A(i+21,:) = A(ii,:);
end
A(length(a)+1,:) = A(1,:);
B(:,:) = B(:,:)+A(:,:)
end
This will obviously rely on the files containing similar enough data, but that would have applied even for your solution.
HTH
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!