Text in a Double array

I have a 12x301 double matrix where the first column is all zeroes and the rest are numerical data. I am trying to get dates to show up in the first column, but apparently you can't insert characters or strings into a double matrix (error message: Subscripted assignment dimension mismatch). I am using Matlab 2011, any help would be greatly appreciated.

4 个评论

You can't store multiple elements into a single element, nor can you mix data types. The closest you can get is with a cell array. Each cell is a container that can contain any variable type and size, or can be left empty.
What is your end goal? What do you want to achieve? We might be able to get you there without breaking the fundamental rules of how Matlab variables work.
I am currently using ncgeodataset to extract data from an online index. From there I am extracting the area of data I need based on latitiude and longitude, and then using inverse distance weighted interpolation to find values for specific points. Finallly, my end goal is to be able to export this data into an excel spreadsheet, with the first column representing the date this data occured on over 10 days (the first two rows represent lat and lon).
Hold your dates in a separate cell array of character vectors. Do your numeric manipulation as needed. Just before writing out, use
datacell = [YourCellOfDates, num2cell(YourNumericData)];
now xlswrite datacell
Thank you! Helped me out

请先登录,再进行评论。

回答(1 个)

You cannot do that with a double array if the dates are represented as characters; you would have to represent the dates numerically such as 201705221933 for May 22 2017 19:33
In R2013b, the table() datatype was created that permits mixed data types in a tabular form.
You could create a cell array, but the display will probably be a bit ugly:
[{'07-Jun-2019'}, num2cell(1:10)]
ans =
1×11 cell array
{'07-Jun-2019'} {[1]} {[2]} {[3]} {[4]} {[5]} {[6]} {[7]} {[8]} {[9]} {[10]}
(The display in your release did not have all of those {} wrappers)
You need to decide the circumstances under which the dates have to be readable. If this is for display onto the graphics window then you could use uitable() . If this is for writing in a file, then you would use fopen() / fprintf() / fclose with appropriate formats, with a loop sometimes being the easiest way to get the formats right. If this is for display to the command window, then write a small routine that does the formatting for you using fprintf() . But if this has to show up nicely when using the variable browser or when using disp() then you have a problem unless you use the numeric date forms.

类别

帮助中心File Exchange 中查找有关 Cell Arrays 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by