How to do Conversion of numeric values to string within cell array?

조회 수: 2 (최근 30일)
Matt b
Matt b 2018년 6월 27일
댓글: Guillaume 2018년 6월 27일
Dear MATLAB experts,
I have a cell array of dimensions {1x162} where each column is a string value. The "strings" are read from a data file containing alpha numeric characters (e.g., data looks like: 1, 2, 3, NA, NA, NA, 4, 5, -.20, -.25, -.26, NA, NA, etc).
I am updating elements of this cell array by replacing values with new ones. When do I this, the elements in the cell array appear like so:
'1' '2' '3' 'NA' 'NA' NA' '4' '5' 1x1cell 1x1cell 1x1cell 'NA' 'NA' etc, where the values I am replacing in this cell array do not take the same format as the other values which I assume are being treated as strings, and the values that I am replacing into this array are indeed numeric.
How can I get MATLAB to treat these new values as the same format as the contents already there? Is there an easy way to input these values as strings rather than numeric? In other words, how can I convert the contents of the 1x1 cells that have appeared within my cell array to appear as the other entries, with '' around them. I have poured over several different ways of doing this and I can't seem to get any of them to work.
Any help that you can provide is greatly appreciated.
Thank you,
-Matt
  댓글 수: 5
Matt b
Matt b 2018년 6월 27일
If you think it would help, here it is. Thanks.
Guillaume
Guillaume 2018년 6월 27일
If what you are doing is replacing some elements of line 18 of a text file, then yes you are overcomplicating it.
If I understand correctly the replacement come from cell array psct which it seems contains numeric data (in which case, the cell array would be better as a matrix for efficiency).
You list elements 109 to 162 (= 54 elements) of the original line to be replaced by elements 1 to 24 (= 25 elements) of psct. Something doesn't tally. How are the elements to be replaced exactly determined?
The whole thing can be done in very few lines once we have all the details.

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

답변 (1개)

Image Analyst
Image Analyst 2018년 6월 27일
Why not just use strsplit:
data = importdata('data.txt', ' ', 18)
str = data{end} % Extract last line
ca = strsplit(str, ' ') % Load into cell array

카테고리

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