Dealing with Duration format
25 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I have a file containing the values of the position of the body's center of pressure, calculated during the trial. I would like to plot the time elapsed (which has the format '0:0:0:064') vs the position. When I open the file (.xlsx) with "readtable", it reads me all the values as "string" and not numbers. I tried to solve this with "str2double", and while this works for the position column, is useless for the time elapsed. When I change the extension (i.e. .csv orb .txt) it converts all the durations into NaNs, even though in the "Import Data" mask it is written at the top of the time elapsed column "duration". As a result, I cannot plot the time on the x-axis.
If I run this piece of code for example:
data = readtable("AdaptationTestProva.xlsx");
x = str2double(data.x_StaticVR_RawMediolateralAxis);
y = str2double(data.TimeElapsed);
plot(y,x)
I get an empty graph.
What can I do to get a standard graph with a usable elapsed time?
If this can be helpful, I'll leave attached an example of the file.
1 Commento
dpb
circa 2 ore fa
"the time elapsed (which has the format '0:0:0:064') "
How is this supposed to be interpreted? It uses the colon as a field separator for what appears to be fractional seconds.
It is an invalid duration format that can't be parsed as written if it is intended to represent hh:mm:ss.SSS.
You would have to read as a string field and do an internal conversion to replace the last colon with a period in order to parse the field as a MATLAB duration.
The better solution would be to fix the broken code that created the bad formatting for any future use. Perhaps the simplest here would be to open the Excel file and do a character substitution there.
Risposte (2)
Steven Lord
circa 3 ore fa
Use detectImportOptions to see how MATLAB would decide to import the data by default based on the contents of the file. Once you have the options object, you can change properties of the import options object to change how MATLAB imports the data.
opts = detectImportOptions("patients.xls")
For example, let's say you wanted to read in this spreadsheet but have Gender specified as a categorical array rather than a char vector. Since it's the second variable in the data, use setvartype with the options object, the variable number 2, and the desired type as inputs.
opts = setvartype(opts, 2, 'categorical');
Now when we import the data, the Gender variable is a categorical array.
data = readtable("patients.xls", opts);
head(data) % Show just the first few rows
class(data.Gender)
If I'd used the default options, MATLAB would have read Gender as text data (a cell array of char vectors) rather than categorical.
data2 = readtable("patients.xls");
head(data2)
class(data2.Gender)
The VariableOptions can give you additional control over how certain inputs are handled. For example, if you had a column in your data that had a fixed set of values and those values had a specified order ('small', 'medium', 'large') you could change the Categories and Ordinal properties of the object to make MATLAB respect that fixed set and the ordering rather than defining the categories in alphabetical order ('large', 'medium', 'small').
optionsForLastName = getvaropts(opts, 1)
optionsForGender = getvaropts(opts, 2)
1 Commento
dpb
10 minuti fa
Unfortunately, Steven, there isn't enough flexibility in the input formatting allowed for a duration to handle the malformed case of the OP's data -- I tried to override but one gets
d=dir('*.csv');
opt=detectImportOptions(d.name);
getvaropts(opt,'TimeElapsed')
opt=setvaropts(opt,'TimeElapsed','InputFormat','hh:mm:ss.SSS','DecimalSeparator',':');
tData=readtable(d.name,opt);
head(tData)
getvaropts(opt,'TimeElapsed')
It still can't parse the duration...if one tries to set the colon in the format string in case it's looking to make the substitution from the literal string, then
opt=setvaropts(opt,'TimeElapsed','InputFormat','hh:mm:ss:SSS','DecimalSeparator',':');
An approach such as @Stephen23 took is the only way one will be able to import this particular (malformed, granted) file.
BTW, simply trying to read with cellmat wasn't successful either, at least without mucking around. I figured that would not have any issues in returning the first column as a cellstr but it also fails...even it wants to import it as a duration.
Stephen23
circa 2 ore fa
Modificato: Stephen23
circa 2 ore fa
F = 'AdaptationTestProva.csv';
H = readtable(F, 'Delimiter',',', 'Range','1:2');
T = readtable(F, 'Delimiter',{',',':'}, 'HeaderLines',1, 'MissingRule','omitrow');
T = renamevars(T, 5:11, H.Properties.VariableNames(2:8))
D = duration(T.Var1,T.Var2,T.Var3,T.Var4); % check the units
plot(D, T.x_StaticVR_RawMediolateralAxis)
0 Commenti
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
