opts.VariableTypes for any number of variables

69 次查看(过去 30 天)
Hi, I want to be able to import a table with ANY number of columns into my MATlab code.
opts.VariableTypes = ["double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double", "double"];
With the above line, readtable works perfectly on my 25-variable (25-column) .csv file. My entire gui works exactly as I need, but only on this exact number of variables.
However, I need to be able to import ANY number of variables, all as doubles. Everything I've tried has given me error messages when I try to plot the imported table in my GUI later on. Three of the many things I tried are below. I'm sure some of them look really dumb to anyone who knows MATlab- I apologize, I was just trying some things out on my own.
This one did not work:
opts.VariableTypes = "double";
Neither did this one, which I tried because all of my variable names start with D:
opts = setvartype(opts,{'D*'},{'single','string'});
Nor did this one:
opts.VariableTypes = ["double", ... ];
  1 个评论
Simon
Simon 2022-8-15
编辑:Simon 2022-8-15
I have the same problem. The help content of 'htmlImportOptions' recommends this method, and it works for me:
opts = setvartype(opts,"string");
This also works:
opts = setvaropts(opts, 'type','string');
One method you mentioned also works for me when delimited..... line is commented out and keep
numVariables = length(opts.VariableNames);
opts.VariableTypes = repmat("string", 1 , numVariables);

请先登录,再进行评论。

采纳的回答

Steven Lord
Steven Lord 2019-8-8
opts.VariableTypes = repmat("double", 1, 25);
Change the number 25 in that line of code as appropriate for your file.
  12 个评论
Walter Roberson
Walter Roberson 2023-5-9
@Yulong Li do you have a fixed number of variables, or a varying number?
In the case where you happen to have a fixed set of variable names, I would suggest readmatrix(), possibly followed by array2table() depending what you are doing.
Yulong Li
Yulong Li 2023-5-16
Unfortunately the number of variables and the sequence of variables changes depending on the tests performed (those are RAM data logs from a debugger).
I did have a work around which is reading in the table as is and then converting the data type in the table. But it's not very efficient and can take a long time to process a big data log, that's why I'm trying to see if I can decide the data type at the time of readtable.

请先登录,再进行评论。

更多回答(1 个)

Tamara del Águila
opts = detectImportOptions("C:\Users\corrine\Desktop\scaracsv.csv");
numVariables = length(opts.VariableNames);
opts = delimitedTextImportOptions;
opts.VariableTypes = repmat("double", 1 , numVariables);
This does not work for me : (
I am using version R2019a. I am importing data from a csv with 'readtable' and also need to import a previously unknown number of variables of type 'double'. If I delete the line 'opts.VariableTypes', instead of a numeric matrix, I get a cell array...
  1 个评论
Walter Roberson
Walter Roberson 2022-8-15
opts = detectImportOptions("C:\Users\corrine\Desktop\scaracsv.csv");
opts = setvartype(opts, 'double'); %in the case of setting all variables
data = readtable(filenames, opts);

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by