Clear Filters
Clear Filters

rowfun for cell variable 2 string conversion in table. Extracting string segment and converting to integer.

1 view (last 30 days)
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

Accepted Answer

Jan
Jan on 22 Aug 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 Comments
Jan
Jan on 22 Aug 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);
Christos Stefos
Christos Stefos on 25 Aug 2017
Replacing the smart ok_irr_data.Index=str2double(cellstr(strtok(ok_irr_data.day,'-'))); with dayNum = sscanf(sprintf('%s*', dayC{:}), '%d*') equivalent structure led to correct compilation. Thanks Jan Simon my deployed app now works same way it works within matlab.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!