Read and assign data to uitable from text file

3 views (last 30 days)
Hello, I have a uitable in my gui (in GUIDE) where I have 4 columns with 20 rows. When I type a name in the cells of the first column, I need my GUIDE code to import data from a .dat text file database (generated by a Fortran code), search the name in that data and read numbers corresponding to that row from the text file and write them into the other columns in the gui uitable. Afterwards, I want to write a output text file with the contents from the uitable in a specfic format having strings and numbers. Can anyone please help me regarding how to program this in GUIDE? I kindly request to explain the programming steps in detail as I am completely new to GUIDE programming.

Answers (1)

Hari
Hari on 24 Feb 2025
Hi Nikhil,
I understand that you are using MATLAB GUIDE to develop a GUI with a “uitable” where you want to populate data from a .DAT text file based on user input in the first column, and then save the table’s content back to a text file.
I assume your .DAT text file is formatted in a way that each row corresponds to a specific entry with a name and associated numerical data, and that you have a clear format for how you want to save the output file.
In order to achieve this functionality in your GUIDE application, you can follow the below steps:
Read Data from the .dat File:
Use MATLAB functions like “fopen”, “textscan”, or “readtable” to load the data from your .dat file into MATLAB. This will allow you to search through the data for specific names.
fid = fopen('data.dat', 'r');
data = textscan(fid, '%s %f %f %f', 'Delimiter', ',');
fclose(fid);
Search for the Name and Update the “uitable”:
Implement a callback function for the “uitable” that triggers when a name is entered. Search for the name in the loaded data and update the corresponding row in the “uitable” with the retrieved numbers.
function updateTable(hObject, eventdata, handles)
% Get the entered name
enteredName = get(hObject, 'Data');
enteredName = enteredName{eventdata.Indices(1), 1};
% Find the index of the entered name
index = find(strcmp(data{1}, enteredName));
if ~isempty(index)
% Update the table with corresponding numbers
set(hObject, 'Data', {enteredName, data{2}(index), data{3}(index), data{4}(index)});
end
end
Write Data to an Output Text File:
Create a function to write the contents of the “uitable” to a text file when needed. Use “fopen”, “fprintf”, and “fclose” for writing.
function writeToFile(hObject, eventdata, handles)
tableData = get(handles.uitable, 'Data');
fid = fopen('output.txt', 'w');
for i = 1:size(tableData, 1)
fprintf(fid, '%s, %f, %f, %f\n', tableData{i, :});
end
fclose(fid);
end
Integrate Callbacks in GUIDE:
Ensure that the callback functions are linked to the “uitable” events in GUIDE. This can be done in the GUIDE editor by setting the appropriate callback properties.
Testing and Validation:
Test the GUI to ensure that names entered in the first column correctly populate the other columns, and that the data is saved in the desired format.
Refer to the documentation of “uitable” for more details on handling data and callbacks: https://www.mathworks.com/help/matlab/ref/uitable.html
Hope this helps!

Categories

Find more on Symbolic Math Toolbox 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!