Hi,
I have a repeating string as following:
materialIndex [
25, 36, 26, 21,-1,
21, 21, 21, 21,-1,
47, 36, 39,-1,
etc.
materialIndex [
47, 36, 37, 47,-1,
56, 54, 47, 47,-1,
47, 35, 36, 47,-1,
etc.
I would like to number these strings as following or similar:
materialIndex1 [
25, 36, 26, 21,-1,
21, 21, 21, 21,-1,
47, 36, 39,-1,
etc.
materialIndex2 [
47, 36, 37, 47,-1,
56, 54, 47, 47,-1,
47, 35, 36, 47,-1,
etc.
I couldn't find a solution so far to this problem.

댓글 수: 4

Ameer Hamza
Ameer Hamza 2020년 5월 16일
Is this inside a txt file, or the string are loaded in MATLAB?
Daniel Rohrer
Daniel Rohrer 2020년 5월 17일
It's from a text file, and I've loaded the string into MATLAB. Both ways are possible
KSSV
KSSV 2020년 5월 17일
How did you load it into MATLAB?
Daniel Rohrer
Daniel Rohrer 2020년 5월 17일
편집: Daniel Rohrer 2020년 5월 17일
I used following code:
% Convert wrl to txt (ASCII)
files=dir('*.wrl');
for i=1:length(files)
filename=files(i).name;
[pathstr, name, ext] = fileparts(filename);
copyfile(filename, fullfile(pathstr, [name '.txt']))
end
% Open specific file, erase and replace syntax
fid = fopen(filename,'r');
f=fread(fid,'*char')';
fclose(fid);

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

 채택된 답변

Ameer Hamza
Ameer Hamza 2020년 5월 17일
편집: Ameer Hamza 2020년 5월 17일

0 개 추천

There can be several ways to do this. The following shows a method using a regular expression.
First, define a helper function and save it in a file.
function y = helperFun()
persistent n
if isempty(n)
n = 1;
end
y = num2str(n);
n = n+1;
end
then use the following code
str = fileread('test.txt'); % test.txt is name of text file
clear helperFun
new_str = regexprep(str, '(materialIndex)', '$1${helperFun()}')

댓글 수: 2

Daniel Rohrer
Daniel Rohrer 2020년 5월 17일
Thank you, works as intended!
Ameer Hamza
Ameer Hamza 2020년 5월 17일
I am glad to be of help!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

릴리스

R2019a

질문:

2020년 5월 16일

댓글:

2020년 5월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by