Reading in file as cell array in a two dimensional [1x1] structure

I am currently having an issue with importing a file in the proper format for a program that I did not write (and the person who wrote it is long gone). This program was written using the MatLab GUI in mind, but I have to use it via code only (I have no formal Matlab training).
The issue is this: The program expects the input of a certain variable to be a 2-dimensional cell array of string vectors, i.e., a 40x10 cell array (each containing only one element of the array). When using the GUI, import is simple: I simply open the file via 'Import Data', highlight the 40x10 region, and import as a cell array. This creates a 40x10 cell array in the 'Workspace'.
When I try to import the file, the program fails. I believe this is because the file is being imported as 40 10x1 arrays, which is not what the program expects. How do I alter my code to achieve a 40x10 cell array?
CellArray = textscan(FileName,'%s%s%s%s%s%s%s%s%s%s','Delimiter',',');
Above is just one example of the import code I have attempted, but I've tried using 'fscanf' and converting with 'cellstr', but that also doesn't format properly. I've tried just one "%s" but that also doesn't work.
Thank you.

4 Kommentare

If you are using Import Data, then a small number of releases ago the defaults on it changed so that now it imports text as String objects instead of cell array of character vectors. There is a button to change to use cell array of character vectors.
Since there was confusion, I want to re-word my statement, with more information that I have determined since I posted last:
  1. The text file is a 40x10 array of strings
  2. When using the MatLab GUI, I simply use "Import Data" to highlight the 40x10 area and upload as a Cell Array (Text option: String Array; appears as a 40x10 cell array in the Workspace)
  3. When using the 'textscan' MatLab code above (using the "size" command to determine its dimensions), it seems as though it is being imported as a 1x10 array.
Therefore, how do I upload the file as a 40x10 cell array via code (not via the GUI). My title doesn't reflect the context of the question, as I didn't fully understand the issue until now.
Image Analyst
Image Analyst am 4 Jan. 2019
Bearbeitet: Image Analyst am 4 Jan. 2019
Attach your file with the paper clip icon, and anything you know about its format, and we will try to read it in and then give you the code.
When you say
The program expects the input of a certain variable to be a 2-dimensional cell array of string vectors, i.e., a 40x10 cell array (each containing only one element of the array).
do you mean that the program expects the text data file to be of that format? Or is the file something else but needs to manipulate the file into that cell array for use later by the program?
I have attached an example input. The file always has the same format, where the first row has a list of gene names, and the following 39 rows are all gene expression values. For example:
"Gene1 Gene2 Gene3 ... Gene10
1.23 5.41 9.26 ... 4.13
5.23 4.49 4.12 ... 1.57
... "
The program expects the input to be in table format. It takes the first row to obtain the gene names, then converts the remaining 39 rows from a cell array to a matrix (which it can do because the rest are all numbers) and processes the values. Here's the code to that part:
Feature_temp = Features; %remove labels from feature cell array and turn into matrix
Feature_temp(1,:) = [];
Train = cell2mat(Feature_temp);
I've also attached a picture of how the data appears on the "Workspace" when imported through the GUI (see the entry "Features"; this example of the data is 40x173 columns long, but it's the same data format, just with more 'genes'). This is what I'm trying to replicate via code.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Stephen23
Stephen23 am 4 Jan. 2019
Bearbeitet: Stephen23 am 4 Jan. 2019

0 Stimmen

Use the CollectOutput option:
opt = {'CollectOutput',true};
fmt = '%s%s%s%s%s%s%s%s%s%s';
C = textscan(fid,fmt,opt{:});
C = C{1}

1 Kommentar

Yes, this was the missing issue. It was in the description in the "textscan" Matlab page, but I didn't get it by the description. The program is still giving me problems, but I know this particular issue has been resolved. Thank you very much.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Produkte

Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by