필터 지우기
필터 지우기

How to get apostrophe's around each element in a matrix?

조회 수: 6 (최근 30일)
Tom Assendelft
Tom Assendelft 2018년 5월 30일
댓글: Stephen23 2018년 5월 31일
I have a matrix that goes [0001 0002 0003 0004 0005 0006 ... 0130]. I now want to make it into the matrix ['0001' '0002' '0003' '0004' ... '0130']. Is there a way to do this, as I noticed it's very hard to do with a for loop, since Matlab recognises 'any text here' as an input command.
I want to use this for the intents of reading multiple data files using the code:
for i = 1:130
OutputV1 = csvread(sprintf('scope_%s_1.csv', measnr{i})
end
Where at the %s the number given in the matrix should go. If there is any easier way to do this that goes around the problem please let me know as well.
Big thanks in advance
  댓글 수: 1
Stephen23
Stephen23 2018년 5월 31일
"How to get apostrophe's around each element in a matrix?"
The example starts with a numeric matrix (which does not have leading zeros as you show it), and you want to convert it into a char vector/array: this does not occur because some apostrophes are "added" to the numeric array, but because the numeric data was converted into character.

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

채택된 답변

Kai Domhardt
Kai Domhardt 2018년 5월 30일
편집: Kai Domhardt 2018년 5월 30일
When you have a matrix filled with numerical entries, like
numeric_matrix = [1,2;3,4]
you can insert its elements into a char array by using %d
sprintf('scope_%d_1.csv', numeric_matrix)
if you want the number to have a constant length with leading zeros you can use %04d, now the number would print as '0005' for 5 or '0105' for 105
sprintf('scope_%04d_1.csv', numeric_matrix)
You could also always you the method of creating your char array by using num2str, but this method as slower than using sprintf()
['scope_' num2str(numeric_matrix(1),'%04d') '_1.csv']
  댓글 수: 1
Tom Assendelft
Tom Assendelft 2018년 5월 30일
Yess! Thank you very much for the quick and clear answer!

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

추가 답변 (1개)

Jan
Jan 2018년 5월 30일
편집: Jan 2018년 5월 30일
for i = 1:130
OutputV1 = csvread(sprintf('scope_%04d_1.csv', measnr{i})
end
By the way, you cannot have "a matrix that goes [0001 0002 0003 ...". If it is a matrix it contains numbers. But numbers do not heave leading zeros. In your code, you use measnr{i}. This is a cell, as you can see by the curly braces. Then it might contain char vectors. In this case the %s format in sprintf is correct already. So please clarify, what the inputs are. Getting the quotes is not a meaningful task. Quotes are displayed automatically, if you show a string in the command window. This is thought to distinguish e.g. the string '1' from the number 1. But the quotes do not belong to the variable.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by