Use anonymous function to convert value before scatter plot
2 views (last 30 days)
Show older comments
Hi there,
Is it possible to call a function before doing a scatterplot? I have a table with values that represent an uint16. The value represent two different IDs. Using two anonymus function I'm able to get the IDs. I would like to plot first ID (PRN) on the y-axis and for each second ID (satType) a different color, the x-axis represent the time.
Is there a simple way to implent this?
Example value: 1838
Example plot:
My approach is as follows:
for i = 36:(width(rfs_csv_table))
if((i) <= width(rfs_csv_table))
all_plots(2) = subplot(2,1,2);
tmp = rfs_csv_table{:,i};
fktPRN = @(sigID) bitand(uint16(sigID),uint16(255));
fktSatType = @(sigID) bitshift(bitand(uint16(sigID), uint16(7936)),-8);
tmp = arrayfun(fkt,tmp);
scatter(rfs_csv_table{:,1},tmp);
hold on;
end
end
0 Comments
Accepted Answer
Voss
on 16 Aug 2023
Yes, you can call scatter and use the fourth input, which is colors.
It is not necessary to use anonymous functions for this, because bitand and bitshift accept array inputs.
% a random table of data:
rfs_csv_table = table((1:10).',randi(65535,10,1,'uint16'));
% some fixed i, for demonstration:
i = 2;
% temporary variable containing contents of ith column:
tmp = rfs_csv_table{:,i};
% method 1: the anonymous function route:
fktPRN = @(sigID) bitand(uint16(sigID),uint16(255));
fktSatType = @(sigID) bitshift(bitand(uint16(sigID), uint16(7936)),-8);
tmpPRN_anon = arrayfun(fktPRN,tmp);
tmpSatType_anon = arrayfun(fktSatType,tmp);
% method 2: calling bitand and bitshift with vector input,
% instead of using anonymous functions:
tmpPRN = bitand(uint16(tmp),uint16(255));
tmpSatType = bitshift(bitand(uint16(tmp), uint16(7936)),-8);
% the results from the 2 methods are the same:
isequal(tmpPRN_anon,tmpPRN)
isequal(tmpSatType_anon,tmpSatType)
% let's see the values, for verifying that the scatter plot looks right:
disp(tmpPRN)
disp(tmpSatType)
% make a scatter plot, with tmpPRN as y and tmpSatType as colors:
scatter(rfs_csv_table{:,1},tmpPRN,[],tmpSatType,'filled');
colorbar
Also, note that if the columns of data in the table are of type uint16, then there is no need to cast them to uint16 using uint16(tmp) (or uint16(sigID) in the anonymous functions), but I kept it that way here because that's how you had it.
Also, if the width of the table doesn't change inside the loop, then there is no need to check if((i) <= width(rfs_csv_table)) because i will always be less than or equal to the width of the table since that's how the for loop is defined: for i = 36:(width(rfs_csv_table))
0 Comments
More Answers (0)
See Also
Categories
Find more on Discrete Data Plots 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!