rowfun for cell variable 2 string conversion in table. Extracting string segment and converting to integer.
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Christos Stefos
el 22 de Ag. de 2017
Comentada: Christos Stefos
el 25 de Ag. de 2017
Below you can see my code. I am importing an excel file with a column with time stamp strings as cell data type variables. See attached photo.
It is basically a string formatted time stamp. From the whole string I need to extract the first two digits which represent the number of the month day (01 - 31)(check attached pic). I have used the command you see on line :
ok_irr_data.Index=str2double(cellstr(strtok(ok_irr_data.day,'-')));
This command works when running the app in matlab. However, I have used matlab application compiler and packaged this code into a stand-alone app.In the compiled app this command does not work. ok_irr_data.Index values return NaN. I have checked with multiple computers that do not have matlab installed, getting the same issue. Is there a simpler way to perform this two digit extraction/conversion, that might work in the standalone app, as well? Is it possible to convert the cell type data variable in the table to simple string variable, early on ,possibly with rowfun , to avoid complex functions? I have attached a screenshot of the desired effect, that appears when I run the code with matlab. The two first digits from day column , go to Index variable column as integers (omitting possible leading zeros). I just need to modify the complex str2double(cellstr(strtok command so it works in the compiled stand alone app as well .Thanks
%Filter unacceptable data points
rows=((hourly_rawdata.AvgIr>handles.cutoff)&(~isnan(hourly_rawdata.RealEnergy)));
ok_irr_data=hourly_rawdata(rows,:);
%Insert a day index
day_index=zeros(height(ok_irr_data),1);
ok_irr_data.Index=str2double(cellstr(strtok(ok_irr_data.day,'-')));
ok_irr_data.Index
ok_irr_data.AL=day_index; %initialize
for i=1:height(ok_irr_data)
index=uint8(ok_irr_data.Index(i));%make sure index is integer.careful of leading zeros
ok_irr_data.AL(i)=(handles.al_mat.AL(index))/100;
end
0 comentarios
Respuesta aceptada
Jan
el 22 de Ag. de 2017
It is hard to guess which part of your code is failing, because it is expected to work correctly:
test = cell2table({'02-Jun-17'; '05-Jun-17'}, 'VariableNames', {'day'})
str2double(strtok(test.day,'-'))
I'd avoid the smart str2double usually and prefer the more simple:
dayC = strtok(test.day,'-');
dayNum = sscanf(sprintf('%s*', dayC{:}), '%d*')
But this still does not explain the cause of the problems. I do assume, that your code works correctly already, but that the contents of the table column is not, what you are expecting. Either the data is stored in numerical format or in an unexpected format. Please check again, if the inputs are correct.
3 comentarios
Jan
el 22 de Ag. de 2017
I still prefer the idea, that ok_irr_data.day does not contain, what you expect, in the compiled version. Either xlsread might suffer, the current folder may differ and you read another file. Check this by exporting the data to a file:
fid = fopen(fullfile(tempdir, 'Test.txt'), 'w');
fprintf(fid, '%s\n', ok_irr_data.day{:});
fclose(fid);
Más respuestas (0)
Ver también
Categorías
Más información sobre Tables en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!