Check if figure tab exists

14 次查看(过去 30 天)
MH
MH 2024-9-1
评论: Umar 2024-9-2
Hi, I'm wondering if there is a similiar approach to findobj which allows me to check if a certain tab that I created in figure (Control) exists by searching for an specific substring in the tab name. For example, if I have a FOR loop which creates tabs, and then titles them based on their corresponding column names from an imported table (Tb)
  • Thermo1_Cond1
  • Thermo2_Cond2
  • Thermo3_Cond3
and then in the second iteration of the FOR loop, I load a new imported table with the same column names but instead of it doubling up and creating new tabs within the same figure, is there a way to check if the tab with a substring of choice (for example 'Themo1') already exists and if so, select the tab, load it, and plot the new data on the same plot? and if not, then create a new tab for it?
I've tried multiple things but I can't seem to get it right. Any help would be much appreciated.
  3 个评论
MH
MH 2024-9-1
so in this example, I had initialized a figure and named it "Control" and within the figure, I generated tabs with names as per their column names in the tables imported. The column names across all imported tables are consisted but the data isnt and I want to plot all the columns with the same names in the same plot aka within the same tab
So within the first FOR loop, I generate the figure and assign the tabs their names and plot the first set of data in each corresponding tab, now for the second iteration, I want to FIRST check if the tab exists as per a substring in its name (For example my current tabs are called Thermo1_Cond and Thermo2_Cond2) and I have a new data set for Thermo1_Cond but instead of creating a new tab with a preexisiting name, I want to first search for the first substring to see if there is a tab that exisists with "Thermo1" and if so, I want to select it and load it so that my next data sets can be plotted within that same tab and in the same plot.
Essentially I want to plot all the Thermo1's together, the Thermo2's together, etc using my FOR loop. Hope that makes sense now
Umar
Umar 2024-9-1

Hi @MH,

To implement this functionality, you can utilize MATLAB's findobj function to check for existing tabs that contain a specific substring in their names. The following steps outline the approach. Use findobj to search for existing tab objects that match your substring criteria. If the tab exists, select it; if not, create a new tab. Then, load and plot your new dataset in the appropriate tab.

For more information on findobj function, please refer to

https://www.mathworks.com/help/matlab/ref/findobj.html

Here's a sample implementation that demonstrates this logic:

% Example Setup
figureHandle = figure('Name', 'Control');
tabGroup = uitabgroup(figureHandle);
% Simulated data import loop (for demonstration)
dataSets = {'Thermo1_Cond1', 'Thermo2_Cond2', 'Thermo3_Cond3'};
for i = 1:length(dataSets)
  % Extract substring (e.g., 'Thermo1')
  subStr = 'Thermo1'; % You may also parameterize this
    % Check for existing tab
    existingTab = findobj(tabGroup, 'Title', subStr);
    if isempty(existingTab)
        % If it doesn't exist, create a new tab
        newTab = uitab(tabGroup, 'Title', dataSets{i});
        % Here you would add your plotting code for the new data
        % Example: plot(newTab, ...);
        disp(['Created new tab: ', dataSets{i}]);
    else
        % If it exists, select the existing tab
        tabGroup.SelectedTab = existingTab;
        % Load new data and plot
        % Example: plot(existingTab, ...);
        disp(['Loaded data into existing tab: ', subStr]);
    end
  end

As you can see the code initializes a figure and a tab group. Then, it simulates the data import process. In practice, you would replace the dataSets array with your actual imported data. Afterwards, findobj function looks for tabs within the tabGroup that have a Title matching the substring. If no existing tab is found, it creates a new tab and is set to plot the corresponding data. However, if a tab is found, it selects that tab and plots the new data onto the existing plot.

Hope this answers your question, please let me know if you have any further questions.

请先登录,再进行评论。

采纳的回答

Umar
Umar 2024-9-1

Hi @MH,

To implement this functionality, you can utilize MATLAB's findobj function to check for existing tabs that contain a specific substring in their names. The following steps outline the approach. Use findobj to search for existing tab objects that match your substring criteria. If the tab exists, select it; if not, create a new tab. Then, load and plot your new dataset in the appropriate tab.

For more information on findobj function, please refer to

https://www.mathworks.com/help/matlab/ref/findobj.html

Here's a sample implementation that demonstrates this logic:

% Example Setup
figureHandle = figure('Name', 'Control');
tabGroup = uitabgroup(figureHandle);
% Simulated data import loop (for demonstration)
dataSets = {'Thermo1_Cond1', 'Thermo2_Cond2', 'Thermo3_Cond3'};
for i = 1:length(dataSets)
  % Extract substring (e.g., 'Thermo1')
  subStr = 'Thermo1'; % You may also parameterize this
    % Check for existing tab
    existingTab = findobj(tabGroup, 'Title', subStr);
    if isempty(existingTab)
        % If it doesn't exist, create a new tab
        newTab = uitab(tabGroup, 'Title', dataSets{i});
        % Here you would add your plotting code for the new data
        % Example: plot(newTab, ...);
        disp(['Created new tab: ', dataSets{i}]);
    else
        % If it exists, select the existing tab
        tabGroup.SelectedTab = existingTab;
        % Load new data and plot
        % Example: plot(existingTab, ...);
        disp(['Loaded data into existing tab: ', subStr]);
    end
  end

As you can see the code initializes a figure and a tab group. Then, it simulates the data import process. In practice, you would replace the dataSets array with your actual imported data. Afterwards, findobj function looks for tabs within the tabGroup that have a Title matching the substring. If no existing tab is found, it creates a new tab and is set to plot the corresponding data. However, if a tab is found, it selects that tab and plots the new data onto the existing plot.

Hope this answers your question, please let me know if you have any further questions.

  5 个评论
Umar
Umar 2024-9-2
Hi @MH,
It’s my pleasure.

请先登录,再进行评论。

更多回答(2 个)

Shivam Gothi
Shivam Gothi 2024-9-1
编辑:Shivam Gothi 2024-9-1
Hello @MH,
Thankyou for clearification.
I believe it is possible to achieve the objective you mentioned using MATLAB. To address this issue, I have created a custom function called plot_on_same_tab(), and I have attached the corresponding (.m) file with this answer, for your convenience.
EXPLANATION OF FUNCTION
%This function plots the data from "your_table.xlsx" into different tabs located inside figure "arg1"
arg1 = uitabgroup(); %Creates a figure. (This is "TabGroup" object).
arg2 = readtable("your_table.xlsx");
plot_on_same_tab(arg1, arg2) %Argument 1 (arg1) must be "TabGroup" object.
%Argument must be a "Table" object.
CASE 1
To illustrate its usage, I have used a sample table with columns named "x", "y1", "y2", and "y3", as shown below. As per your request, the goal is to plot ("x" vs "y1"), ("x" vs "y2"), and ("x" vs "y3") on the same figure (named "control") but on separate tabs.
This table is saved in an excel file named "RawData.xlsx".
On the MATLAB command window, type the following code:
control = uitabgroup() %Creates a Figure
T=readtable("RawData.xlsx") %Read table (%Type the name of your table here)
plot_on_same_tab(control,T) %CAUTION : The first variable to this function must be a "TabGroup" object and second element must be a "Table" object
This will create a figure with 3 tabs as shown below (you can see the tabs on top-left corner):
Click on the tab to view the sensor data.
NOTE : DO NOT CLOSE THE FIGURE. IT IS REQUIRED FOR FURTHER STEPS
CASE 2:
Now suppose that the excel file is modefied as shown below: (NOTE: here, I have added additional data to existing sensors and also changed some of the values).
Now again type the following lines in command window:
%Do not close previously generated figure
T=readtable("RawData.xlsx")
plot_on_same_tab(control,T)
Now, open the figure. You will see that no additional tabs were created, but the new data points were plotted in the corresponding existing tab.
CASE 3:
Now, Suppose that I changed the "RawData.xlsx" table to the following: (note that I have added an extra column)
Now, again load the table and call the user defined function by typing following lines in command window:
%Do not close previously generated figure
T=readtable("RawData.xlsx"); %Again read modefied table (%Type the name of your table here)
plot_on_same_tab(control,T); %This will add an additional tab for "y4" sensor data in the figure "control"
Open the figure "control", you will now see an extra tab added on top-left corner as shown below:
NOTE: The tabs (y1, y2, y3) are as it is, because they were already existing. Only a new tab is added.
You can also create two different figures as "control1" and "control2" and plot data of different files as shown below:
control1 = uitabgroup();
T=readtable("RawData.xlsx"); %Type the name of your table here
plot_on_same_tab(control1,T1);
control2 = uitabgroup();
T=readtable("RawData_modefied.xlsx"); %Type the name of your table here
plot_on_same_tab(control2,T2);
This will plot the modified sensor data to the tabs of other figure named "control2", instead of modifying the tabs of "control1" figure.
NOTE: PLACE THE (.XLSX) TABLE FILE AND (plot_on_same_tab.m) FILE ON THE PATH OF MATLAB COMMAND WINDOW EXECUTION.
Please refer to below documentation for reference:
I hope this solves your objective !
  2 个评论
Shivam Gothi
Shivam Gothi 2024-9-1
Hello @MH,
Just to add a few points to the above provided answer.
Here, I am assuming that you are plotting the data of different thermos with respect to time. In this case, your "x" variable (1st column) in the table will be the time steps. (example: 0, 0.001, 0.002, 0.003, 0.004..... secs), and the remaining columns will be the value of the sensor at each time step.
Also, the basic syntax of "Table" should be:
Column 1: it should be compulsorily "x-axis" data. It is common for all sensors.
That is, column2, column3 column4... will be plotted on y-axis (on seperate tabs), against column 1 on x-axis.
You can modify the "plot_on_same_tab.m"(attached in above answer) file according to your requirnments to achieve desired functionality .
MH
MH 2024-9-1
@Shivam Gothi, first off thank you immensley for your efforts. However, the code doesn't fully fullfill what I plan on doing. I followed your steps with my tables and it seems like the plots are overriding one another by plotting a new plot box over another, as opposed to appending the lines on the same plot grid. Also, having more than 4 data columns resulted in duplicate tabs being created.
Essentially, what I'm looking for is more closely related to the comment made by Umar where I am looking for a simple line to check whether or not this tab exists by locating it using a substring and then loading it.

请先登录,再进行评论。


Umar
Umar 2024-9-2

Hi @MH,

To work around this limitation and effectively manage tabs in a MATLAB GUI, I can leverage the findobj function to search for existing tab objects based on their titles. However, going through documentation, it seems that findobj does not support substring matching directly. So, instead, I can utilize the contains function as you suggest in conjunction with a loop to achieve the desired functionality. Below, I will provide a detailed explanation and a refined code implementation that addresses the concerns raised. So, I started creating a figure and a tab group where the tabs will reside. For demonstration purposes, I will simulate the import of datasets. In a real application, this would involve loading your actual data. Instead of using findobj directly to find a tab by title, I will iterate through the existing tabs and use the contains function to check if any tab title includes the specified substring. If a matching tab is found, I will select it; if not, I will create a new tab and plot the corresponding data. Here is a refined version of the code that incorporates the above logic:

% Example Setup
figureHandle = figure('Name', 'Control');
tabGroup = uitabgroup(figureHandle);
% Simulated data import loop (for demonstration)
dataSets = {'Thermo1_Cond1', 'Thermo2_Cond2', 'Thermo3_Cond3'};
for i = 1:length(dataSets)
  % Extract substring (e.g., 'Thermo1')
  subStr = 'Thermo1'; % This can be parameterized as needed
    % Initialize a flag to check if the tab exists
    tabExists = false;
    % Check for existing tabs using contains
    for j = 1:length(tabGroup.Children)
        if contains(tabGroup.Children(j).Title, subStr)
            existingTab = tabGroup.Children(j);
            tabExists = true;
            break; % Exit loop if tab is found
        end
      end
     if ~tabExists
        % If it doesn't exist, create a new tab
        newTab = uitab(tabGroup, 'Title', dataSets{i});
        % Here you would add your plotting code for the new data
        % Example: plot(newTab, ...);
        disp(['Created new tab: ', dataSets{i}]);
     else
        % If it exists, select the existing tab
        tabGroup.SelectedTab = existingTab;
        % Load new data and plot
        % Example: plot(existingTab, ...);
        disp(['Loaded data into existing tab: ', 
        existingTab.Title]);
     end
  end

Please see attached.

For more information on ui tab group function, please refer to

https://www.mathworks.com/help/matlab/ref/uitabgroup.html?searchHighlight=uitabgroup&s_tid=srchtitle_support_results_1_uitabgroup

So, if you glance through the code, it begins by creating a figure and a tab group to hold the tabs. The dataSets array simulates the datasets that would typically be imported and a loop iterates through the existing tabs in the tabGroup. The contains function checks if the title of any tab includes the specified substring (subStr). If a match is found, the corresponding tab is stored in existingTab, and a flag (tabExists) is set to true. Depending on whether a matching tab was found, the code either creates a new tab or selects the existing one. The appropriate plotting code can be added where indicated. Hope this should help resolve your problem now.

标签

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by