MATLAB Answers

0

Import txt/csv into a cell of a column

Asked by Tobias Wzl on 17 Oct 2017
Latest activity Answered by Arvind Narayanan on 23 Oct 2017
Hello everyone,
this is my code I'm struggling with:
% --- FIGURE -------------------------------------
handles.figure1 = figure( ...
'Tag', 'figure1', ...
'Units', 'characters', ...
'Position', [102.8 24.2307692307692 126.8 33], ...
'Name', 'Parameter', ...
'MenuBar', 'figure', ...
'NumberTitle', 'off', ...
'Color', [0.941 0.941 0.941]);
% --- UITABLE -------------------------------------
% Initialize empty string for components of the Data
Data=cell(16,5);
for i = 1:numel(Data)
Data{i} = '';
end
uitable1 = uitable( ...
'Parent', handles.figure1, ...
'Tag', 'uitable1', ...
'UserData', zeros(1,0), ...
'Units', 'characters', ...
'Position', [12.2 8 85.6 21], ...
'BackgroundColor', [1 1 1;0.961 0.961 0.961], ...
'ColumnEditable', [true,true,true,true,true], ...
'ColumnFormat', {'char','char','char','char','char'}, ...
'ColumnName',{'ID','<html>P<sub>i</sub> -stationary<br>[W]','<html>P<sub>i</sub> -transient<br>[W/t]','<html>&Omega','V'}, ... % '<html>P<sub>i</sup></html>[W]'für griechische Buchstaben in einer Column <HTML>&Buchstabe
'ColumnWidth', {'auto','auto','auto','auto','auto'}, ...
'Data',Data); % add the "string" Data
function uitable1_CellEditCallback(hObject, eventdata, handles)
Columndata = get(hObject,'Data');
[FileName,PathName] = uigetfile({'*.txt'; '*.csv'},'Select the configuration');
if strcmp(Columndata(1,1),'User Defined')
FilePath = fullfile(PathName,FileName);
if isequal(FileName,0)
return
end
[pathstr, name, ext] = fileparts(FilePath);
ParameterList{end+1} = name;
set(uitable1,'ColumnFormat',{ParameterList});
end
end
which command(s) do I need so I can import my txt or csv File into a cell of a column (should work for all the cells of the column P_i-transient)
tried it with importdata or fopen and it didnt work for me, I think I used it wrong :( can somebody help me with the right command(s) and where to put them?

  2 Comments

Note that rather than using a loop:
Data=cell(16,5);
for i = 1:numel(Data)
Data{i} = '';
end
all you need is some indexing:
Data = cell(16,5);
Data(:) = {''};
or even simply
Data = repmat({''},16,5);
I corrected this, thank you.
I got it done now, that if I click on any cell a menu appears where I can select my file. But I just want this for a specific column (P_i-transient (should be column 3 I think)) and I also want that the filename appears in the cell in the colour blue
CODE:
function uitable1_CellSelectionCallback(hObject, eventdata)
datatable_row = eventdata.Indices(1);
datatable_col = eventdata.Indices(2);
Columndata = get(hObject,'Data');
[FileName,PathName] = uigetfile({'*.txt'; '*.csv'},'Select the configuration');
if strcmp(Columndata(1,1),'User Defined')
FilePath = fullfile(PathName,FileName);
if isequal(FileName,0)
return
end
[pathstr, name, ext] = fileparts(FilePath);
ParameterList{end+1} = name;
set(uitable1,'ColumnFormat',{ParameterList});
end
end

Sign in to comment.

1 Answer

Answer by Arvind Narayanan on 23 Oct 2017
 Accepted Answer

Hi Tobias,
Although there is no direct way to assign different callbacks for cells from different columns, you may use the eventdata structure from the CellEditCallback and the CellSelectionCallback to find the row and column indices of the cell being selected. Following this, you can define some conditional loop statements so that the behavior of the callback differs depending upon the row or column index( as per your requirement).
To control the color of the text that appears in the cell, change the Foreground Color property from within the above-mentioned callback itself.

  0 Comments

Sign in to comment.