I'm trying to wrap my head around how the arrayfun() function works and would greatly appreciate some help with a specific example:
I have a string array of weather data.
weather_strings = 10×1 string array "UTC,2140991,49.0" "UTC,2140992,49.1" "UTC,2140993,49.1" ...
I need to extract the values after the second comma (temperatures) as a 1×10 matrix of doubles, [49.0, 49.1, 49.1, …].
I've figured out a clunky way to do this for a single entry (please let me know if there's a better way).
weather_string = weather_strings(1) % extract only the first entry
weather_string_split = strsplit(weather_string, ',') % apply strsplit() to split on commas
weather_string_split_trim = weather_string_split(:,3) % extract only 3rd column
weather_num_trim = str2num(weather_string_split_trim) % convert from string to double
But I can't seem to figure out how to use arrayfun() to apply that to every entry. I've tried:
weather_strings_split = arrayfun(strsplit(weather_strings,','), weather_strings) % apply stringsplit to split on commas, for all elements?
which gives the error message:
Error using strsplit (line 80)First input must be either a character vector or a string scalar.Error in test_window (line 17)weather_strings_split = arrayfun(strsplit(weather_strings,','), weather_strings)
I'm probably missing something painfully obvious. What is it? I'm still somewhat of a beginner at coding, so I welcome you to explain it to me like I'm 5 years old.
Alternatively, if there's a clever way to extract these numbers directly from this data table (which came directly from a webread() function), I'd love to hear it. Var3 is a cell array.
weather_data_table = 10×3 table Var1 Var2 Var3 __________ ________ __________________ 2018-11-26 17:41:25 'UTC,2140991,49.0' 2018-11-26 17:42:27 'UTC,2140992,49.1' 2018-11-26 17:43:28 'UTC,2140993,49.1'...
Again, the goal is to get just the last numbers after the second comma of Var3 into a 1D matrix.
Thanks in advance!
Best Answer