Import txt/csv into a cell of a column

1 visualizzazione (ultimi 30 giorni)
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 Commenti
Stephen23
Stephen23 il 17 Ott 2017
Modificato: Stephen23 il 17 Ott 2017
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);
Tobias Wzl
Tobias Wzl il 18 Ott 2017
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

Accedi per commentare.

Risposta accettata

Arvind Narayanan
Arvind Narayanan il 23 Ott 2017
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.

Più risposte (0)

Categorie

Scopri di più su Dialog Boxes in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by