What variable can I put into find()?

21 ビュー (過去 30 日間)
Lee Lewis
Lee Lewis 2022 年 6 月 23 日
回答済み: dpb 2022 年 6 月 23 日
I'm struggling to figure out what variable to put into find(). The goal is to put in the mouses x-cordinate location into the graph with the goal of extracting ALL of the correlating y values for the row#. I will post the code below.
This is the working code:
%clear
clear;
%recieve file
%have user select a file with pop up window
%Can only grab CSV files
[file,path,indx] = uigetfile(".csv", 'Select a file');
Error using matlab.internal.lang.capability.Capability.require
Support for Java user interfaces is required, which is not available on this platform.

Error in uigetfile (line 117)
Capability.require(Capability.Swing);
%modify with opts to get rid of NaNs
opts = detectImportOptions(file);
opts = setvartype(opts, 'char');
%read the table
data = readtable(file,opts);
%get the number of rows and columns
numRows = height(data);
numCols = width(data);
%extracting Header titles for describing the frequency that the antennas are
%Transmitting and recieving at.
titleTx = data{1,1};
titleTxValue=data{1,2};
titleHz=data{1,3};
titleRx=data{2,1};
titleRxValue=data{2,2};
titleHz2=data{2,3};
%Turn 1x1 cell arrays into strings in order to make title
titleTxString = char(titleTx);
titleTxValueString = char(titleTxValue);
titleHzString = char(titleHz);
titleRxString = char(titleRx);
titleRxValueString = char(titleRxValue);
titleHz2String = char(titleHz2);
%Combine title values in order to put in one line on graph
titleCombo = {titleTxString ,titleTxValueString,titleHzString, titleRxString, titleRxValueString, titleHz2String};
str = strjoin(titleCombo);
%generate figure
f1 = figure(1);
cla; hold on; grid on;
xAxisTitleFreq = data{3,1};
xAxisDataFreq = cellfun(@str2num,data{4:end,1});
%for loop for generating data under headers i.e(frequency, EMI, etc.)
for i=2:numCols
%Extract data from readData
%converts yaxisDataPwr string to a double
yAxisDataPwr = str2double(data{4:end,i});
%grab data for legend
legendHeaderNames = char(data{3,i});
%Plot data
plot(xAxisDataFreq, yAxisDataPwr,'DisplayName', legendHeaderNames);
end
%data Cursor mode, this allows the data point closest to the cursor to be
%displayed after the user clicks the left mouse button.
dcm = datacursormode;
dcm.Enable = 'on';
dcm.DisplayStyle = 'window';
%labeling x&y axis and title
% y-axis is is db but some data points are in dBM, may need to go back and
% try and modify IDK tho.
title(str);
xlabel(xAxisTitleFreq);
ylabel("dB");
lgd=legend;
legend show;
%modifies the number of columns in the legend
lgd.NumColumns = 3;
This is the code that I am trying to add, the issue is that MATLAB isn't recognizing C(1,1) as a variable (which i'm pretty sure is the x cordinate of the mouse location).
%generates table, will use for interperlation
bTest = data{4:end,1:end};
%Take firsrt column (frequency values) from bTest
firstCol = bTest(:,1);
%find still figuring this out
k=find(C(1,1)<firstCol(end));
m=[k(end),k(end)+1];
%Testing purposes: trying to get a and y cordinates
fig2 = uifigure;
holder = [0,0];
tab = uitable(fig2,'Data',holder,'ColumnName',{'X Coord','Y Coord'});
set (gcf, 'WindowButtonMotionFcn', {@mouseMove,tab});
function mouseMove (src,event,tab)
C = get (gca, 'CurrentPoint');
title(gca, ['(X,Y) = (', num2str(C(1,1)), ', ',num2str(C(1,2)), ')']);
MousePt = [C(1,1),C(1,2)];
tab.Data = MousePt;
end
ANY help would be greatly appreciated!

採用された回答

dpb
dpb 2022 年 6 月 23 日
" MATLAB isn't recognizing C(1,1) as a variable (which i'm pretty sure is the x cordinate of the mouse location). "
There is no reference to a variable C in the above code prior to that line -- it ("C") is ONLY a local variable in the callback function -- as soon as the callback function terminates, C goes out of scope and is not available anywhere except inside the callback. Hence, the error -- C simply does not exist in the user code, only in the callback.
the data structure tab contains the array, however; that's what you should be referencing.
function mouseMove (src,event,tab)
C = get (gca, 'CurrentPoint');
title(gca, ['(X,Y) = (', num2str(C(1,1)), ', ',num2str(C(1,2)), ')']);
tab.MousePt=C(1,1:2);
end
I modified your callback routine a little to make the struct field name somewhat more meaningful and a tiny bit more legible/efficient by using colon indexing to get the first two elements of the returned position array instead of the [] and explicit element addresses.

その他の回答 (0 件)

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by