Azzera filtri
Azzera filtri

opts.VariableTypes for any number of variables

79 visualizzazioni (ultimi 30 giorni)
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 Commento
Simon
Simon il 15 Ago 2022
Modificato: Simon il 15 Ago 2022
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);

Accedi per commentare.

Risposta accettata

Steven Lord
Steven Lord il 8 Ago 2019
opts.VariableTypes = repmat("double", 1, 25);
Change the number 25 in that line of code as appropriate for your file.
  12 Commenti
Walter Roberson
Walter Roberson il 9 Mag 2023
@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 il 16 Mag 2023
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.

Accedi per commentare.

Più risposte (1)

Tamara del Águila
Tamara del Águila il 3 Giu 2020
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 Commento
Walter Roberson
Walter Roberson il 15 Ago 2022
opts = detectImportOptions("C:\Users\corrine\Desktop\scaracsv.csv");
opts = setvartype(opts, 'double'); %in the case of setting all variables
data = readtable(filenames, opts);

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by