opts.VariableTypes for any number of variables
67 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Corrine Rybarski
le 8 Août 2019
Commenté : Yulong Li
le 16 Mai 2023
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 commentaire
Simon
le 15 Août 2022
Modifié(e) : Simon
le 15 Août 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);
Réponse acceptée
Steven Lord
le 8 Août 2019
opts.VariableTypes = repmat("double", 1, 25);
Change the number 25 in that line of code as appropriate for your file.
12 commentaires
Walter Roberson
le 9 Mai 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
le 16 Mai 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.
Plus de réponses (1)
Tamara del Águila
le 3 Juin 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 commentaire
Walter Roberson
le 15 Août 2022
opts = detectImportOptions("C:\Users\corrine\Desktop\scaracsv.csv");
opts = setvartype(opts, 'double'); %in the case of setting all variables
data = readtable(filenames, opts);
Voir également
Catégories
En savoir plus sur Spreadsheets dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!