opts.VariableTypes for any number of variables
조회 수: 64 (최근 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
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
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
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
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
2020년 6월 3일
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
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);
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!