필터 지우기
필터 지우기

How to write string loops?

조회 수: 1 (최근 30일)
Ebadur
Ebadur 2014년 7월 21일
답변: Dinghuan Zhu 2014년 7월 22일
How can I write a loop exact in the same way its given below. For every loop the abc1 and node 1 will change:
I should get a text in this exact format
O: cd O:\abc\result
C:\xyz abc1.txt out=result_node1
O: cd O:\abc\result
C:\xyz abc2.txt out=result_node2
  댓글 수: 1
Azzi Abdelmalek
Azzi Abdelmalek 2014년 7월 21일
This is not clear

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

답변 (1개)

Dinghuan Zhu
Dinghuan Zhu 2014년 7월 22일
You can use the function “sprintf” to format data into string in MATLAB. The following code generates the exact result you are looking for.
N = 2; % number of iterations
str = []; % initialize the output string
str_h = ['O: cd O:\abc\result' char(10)]; % header string including newline
for i = 1: N
str_i = sprintf('C:\\xyz abc%d.txt out=result_node%d', i, i);
str = [str str_h str_i];
if i ~= N
str = [str char(10)]; % char(10) is the newline char
end
end

카테고리

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