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

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 Comments

The best approach would be to fix your code so that it does the right thing straight away, rather than fixing the mess after the fact.
For that, we'd need to see your code (and an example of input would also help).
Yes, if I was a better programmer I would not run into these types of problems I am sure. I am self-taught on MATLAB over the last month for my dissertation and this issue is the last problem I have to solve. So please forgive my inexperience. I am not sure what code I could paste that is relevant, but here it goes:
Data are first read in from a file that is structured in a complex way to read out only one line of interest like this:
fid=fopen('G:\DTEST\data.txt');
tline = fgetl(fid);
while ischar(tline)
tlines{end+1,1} = tline;
tline = fgetl(fid);
end
fclose(fid);
workthis = tlines(18,1);
c = regexp(workthis, ' ','split');
This grabs the specific line I need and splits it up into strings. Then I do the following:
c{1,1}{1,109} = psct{1,1};
c{1,1}{1,110} = psct{1,2};
.
.
.
c{1,1}{1,162} = psct{1,24};
to update information in this cell array with contents of another cell array that I have gotten as results from another portion of the program. When I do this, instead of getting similarly formatted data I get 1x1 numeric cells in c.
Yes, I am aware that I am not good at indexing.
The next step is to write this data out to a text file, and the fprintf function that I have set up will recreate the complex structured text file exactly as I need it, and will replace the data line that I need to with the updated contents that I want, but will not work unless all of the characters are strings.
Hence why I am asking if there is an easy way to just convert the cell array contents I have changed to a string or not. I am certain there is a better way to do this, but if there is a way to easily convert these cell contents to a string the problem will be solved as well.
Thanks for your time and consideration.
Did you forget to attach 'G:\DTEST\data.txt'???
You say you're getting numeric values but you want strings, so why can't you just use num2str() or sprintf()? Just loop and set c{k} = num2str(c{k}).
If you think it would help, here it is. Thanks.
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.

Sign in to comment.

Answers (1)

Why not just use strsplit:
data = importdata('data.txt', ' ', 18)
str = data{end} % Extract last line
ca = strsplit(str, ' ') % Load into cell array

Categories

Asked:

on 27 Jun 2018

Commented:

on 27 Jun 2018

Community Treasure Hunt

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

Start Hunting!