81 views (last 30 days)
Show older comments
MH on 1 Sep 2024 at 0:37
Commented: Umar on 2 Sep 2024 at 2:22
Accepted Answer: Umar
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 Comments Show 1 older commentHide 1 older comment
Show 1 older commentHide 1 older comment
Shivam Gothi on 1 Sep 2024 at 2:26
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2149334-check-if-figure-tab-exists#comment_3250884
Edited: Shivam Gothi on 1 Sep 2024 at 2:28
Hello @MH,
According to my understanding,
you are creating tab objects inside figure (generated by uicontrol) with names "Thermo1_cond1", "Thermo2_cond2", "Thermo3_cond3".
But, what are you trying to achieve after importing new table? can you please elaborate, or give a small example?
MH on 1 Sep 2024 at 6:15
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2149334-check-if-figure-tab-exists#comment_3250909
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 on 1 Sep 2024 at 22:35
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2149334-check-if-figure-tab-exists#comment_3251389
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 SetupfigureHandle = 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.
Sign in to comment.
Accepted Answer
Umar on 1 Sep 2024 at 22:36
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 SetupfigureHandle = 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 Comments Show 3 older commentsHide 3 older comments
Show 3 older commentsHide 3 older comments
MH on 1 Sep 2024 at 23:27
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2149334-check-if-figure-tab-exists#comment_3251404
Hi @Umar, the line 'existingTab = findobj(tabGroup, 'Title', subStr)' is not working as expected. It returns a 0 even if the tab exists. You can see this if you run your script once as is, and then comment out the first two lines and then run it again, you see it creates 3 extra tabs.
MH on 1 Sep 2024 at 23:57
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2149334-check-if-figure-tab-exists#comment_3251419
@Umar I guess it would only work if I feed the entire name of the tab as opposed to a substring. Is there where I use a "contains()" or something similar within the findobj argument?
Umar on 2 Sep 2024 at 0:50
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2149334-check-if-figure-tab-exists#comment_3251439
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 SetupfigureHandle = 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
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.
MH on 2 Sep 2024 at 1:20
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2149334-check-if-figure-tab-exists#comment_3251444
@Umar THANK YOU!! finally
Umar on 2 Sep 2024 at 2:22
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2149334-check-if-figure-tab-exists#comment_3251459
Hi @MH,
It’s my pleasure.
Sign in to comment.
More Answers (2)
Shivam Gothi on 1 Sep 2024 at 11:38
Edited: Shivam Gothi on 1 Sep 2024 at 11:45
Open in MATLAB Online
- plot_on_same_tab.m
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:
https://in.mathworks.com/help/matlab/ref/uitabgroup.html
I hope this solves your objective !
2 Comments Show NoneHide None
Show NoneHide None
Shivam Gothi on 1 Sep 2024 at 12:21
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2149334-check-if-figure-tab-exists#comment_3250969
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 on 1 Sep 2024 at 23:40
Direct link to this comment
https://www.mathworks.com/matlabcentral/answers/2149334-check-if-figure-tab-exists#comment_3251414
@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.
Sign in to comment.
Umar on 2 Sep 2024 at 0:51
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 SetupfigureHandle = 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
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.
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABData Import and AnalysisData Import and ExportStandard File FormatsSpreadsheets
Find more on Spreadsheets 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!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office