How to read values from a table or Excel File and assign it to a textbox

3 visualizaciones (últimos 30 días)
Ahmed Jubore
Ahmed Jubore el 20 de Feb. de 2021
Respondida: Sameer el 18 de Sept. de 2024
Hello
I have an excel file named ("fileName.xlsx") it contains two columns, the first column contain a file name like (cow.png, car.jpg ... etc) and the second column contain a number value like ( 1,2,3...etc). in GUI I have a push button to get the file and displayed it in figure and one textbox. so how can I display the value in the second column in the textbox when the names in the first column matched with the opened filename otherwise displayed not found?

Respuestas (1)

Sameer
Sameer el 18 de Sept. de 2024
Hi Ahmed
To display the value from the ‘second’ column of an Excel file in a textbox when the file name from the ‘first’ column matches a selected image file, you can create a GUI with a “push button” and a “text box”.
Here’s how you can achieve this:
1. Create the GUI: Add a push button and a text box to your layout.
2. Implement the Callback Function for the Push Button:
  • When the push button is clicked, it will prompt the user to select an image file.
  • It will then check if the selected file name matches any entry in the Excel file and display the corresponding value in the text box. If no match is found, it displays "Not Found".
Here is a sample callback function:
function pushbutton_Callback(hObject, eventdata, handles)
% Prompt user to select an image file
[fileName, filePath] = uigetfile({'*.png;*.jpg;*.jpeg', 'Image Files (*.png, *.jpg, *.jpeg)'}, 'Select an Image File');
if isequal(fileName, 0)
disp('User canceled file selection');
return;
end
% Read the Excel file
data = readtable('fileName.xlsx');
% Extract the file names and corresponding values
excelFileNames = data{:, 1};
excelValues = data{:, 2};
% Find the index of the selected file name in the Excel data
matchIdx = strcmp(excelFileNames, fileName);
% Display the image in a figure
fullFileName = fullfile(filePath, fileName);
img = imread(fullFileName);
figure;
imshow(img);
title(['Displaying: ', fileName]);
% Display the corresponding value in the textbox
if any(matchIdx)
% Get the corresponding value
value = excelValues(matchIdx);
set(handles.textbox, 'String', num2str(value));
else
% Display "Not Found" if no match is found
set(handles.textbox, 'String', 'Not Found');
end
end
Hope this helps!

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by