Import txt/csv into a cell of a column

1 次查看(过去 30 天)
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 个评论
Stephen23
Stephen23 2017-10-17
编辑:Stephen23 2017-10-17
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 2017-10-18
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

请先登录,再进行评论。

采纳的回答

Arvind Narayanan
Arvind Narayanan 2017-10-23
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 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by