■enum
classdef Status
enumeration
Idle
Running
Error
end
end
■キャッシュのクリア
evalin('base','clear variables');
evalin('base','clear functions');
clear java %注意が必要
clear classes
clear mex
rehash toolboxcache
■アプリのフォルダを知りたい
appFolder = fileparts(which(class(app)));
■UItable 行ごとに色を変える
s = uistyle('BackgroundColor',[1.0 0.9 0.9]); % ピンク
addStyle(app.UITable,s,'row',3);
addStyle(app.UITable,s,'row',5);
色を解除する場合
removeStyle(app.UITable);
■UITable ColumnEditのX列目のみtrueとfalseを切り替える
editable = logical(app.UITable.ColumnEditable);
editable(X) = condition;
app.UITable.ColumnEditable = editable;
※ColumnFormatなども同様
■uigetfileでUIFigureが裏に隠れてしまう件の対処
ファイル選択後に明示的にフォーカスを戻します。
fig = uifigure;
[file,path] = uigetfile('*.*');
figure(fig) % または drawnow
■uigetfileでUIFigureが裏に隠れてしまう件の対処
fig = uifigure;
[file,path] = uigetfile( ...
'*.*', ...
'ファイル選択', ...
'Parent', fig);
■Ctr+Cでクリップボードにコピー、Ctrl+VでUTableに貼り付け
StartupFcn
function startupFcn(app)
% UIFigureでキー入力を監視
app.UIFigure.WindowKeyPressFcn = ...
@(src,event)keyPress(app,event);
end
% キーイベント
function keyPress(app,event)
if ismember('control',event.Modifier) && ...
strcmp(event.Key,'c')
copyTableSelection(app);
elseif ismember('control',event.Modifier) && ...
strcmp(event.Key,'v')
pasteToTable(app);
end
end
% クリップボードにコピー
function copyTableSelection(app)
idx = app.SelectedCells;
if isempty(idx)
return;
end
data = app.UITable.Data;
r = unique(idx(:,1));
c = unique(idx(:,2));
subData = data(r,c);
txt = "";
for i = 1:size(subData,1)
rowStr = strings(1,size(subData,2));
for j = 1:size(subData,2)
rowStr(j) = string(subData{i,j});
end
txt = txt + join(rowStr,sprintf('\t'));
if i < size(subData,1)
txt = txt + newline;
end
end
clipboard('copy',char(txt));
end
% 貼り付け処理
function pasteToTable(app)
% クリップボード取得
txt = clipboard('paste');
% 行分割
rows = regexp(txt,'\r\n|\n|\r','split');
% 最終空行削除
if isempty(rows{end})
rows(end) = [];
end
% タブ区切り
pasteData = cell(numel(rows),1);
for i = 1:numel(rows)
pasteData{i} = strsplit(rows{i},sprintf('\t'));
end
% UITable取得
data = app.UITable.Data;
% 例:左上から貼り付け
startRow = 1;
startCol = 1;
for r = 1:numel(pasteData)
vals = pasteData{r};
for c = 1:numel(vals)
data{startRow+r-1,startCol+c-1} = vals{c};
end
end
app.UITable.Data = data;
end
■cellの空行を削除(空行 = 全列が [])
emptyRows = all(cellfun(@isempty, data), 2);
data(emptyRows,:) = [];
■cellの空行を削除(空行 = 全列が [] または '')
isEmpty = cellfun(@(x) ...
isempty(x) || ...
(ischar(x) && isempty(strtrim(x))), ...
data);
emptyRows = all(isEmpty, 2);
data(emptyRows,:) = [];
■cellの空行を削除(空行 = []、''、""、NaN をすべて空扱い)
isEmpty = cellfun(@(x) ...
isempty(x) || ...
(ischar(x) && isempty(strtrim(x))) || ...
(isstring(x) && strlength(x)==0) || ...
(isnumeric(x) && isscalar(x) && isnan(x)), ...
data);
emptyRows = all(isEmpty, 2);
data(emptyRows,:) = [];
■cell:行数不明に対する処理
あらかじめ大きめに確保する
(最大行数の見当が付くなら)
maxRows = 20000;
data = cell(maxRows,7);
idx = 0;
for k = 1:N
idx = idx + 1;
data(idx,:) = rowData;
end
data = data(1:idx,:); % 余分な行を削除
例:3列目だけを対象にして「完全一致する行番号を知りたい場合
■ 数値・文字列混在
target = "A"; % 探したい値
matchRows = cellfun(@(x) isequal(x,target), data(:,3));
rowIdx = find(matchRows);
■ 数値only
target = 10;
rowIdx = find([data{:,3}] == target);
■ 文字列中心
rowIdx = find(strcmp(data(:,3), "A"));
または
rowIdx = find(string(data(:,3)) == "A");
■ 複数条件(例:3列目がAかつ数値10など)
rowIdx = find( ...
cellfun(@(x) isequal(x,"A"), data(:,3)) & ...
cellfun(@(x) isequal(x,10), data(:,4)) );
■ cellの列の文字列検索
idx = strcmp(C(:,3), target);
■ X列目から 数値を検索する場合
idx = cell2mat(C(:,X)) == 200;
rows = find(idx);
または
rows = find(cell2mat(C(:,2)) == 200);
■cellの値をwritematrixで書き込む
C = { 100; 200; 300 };
A = cell2mat(C);
writematrix(A,'result.xlsx');
■mlappをEXEにしたときのEXEの場所を取得する方法
if isdeployed
%キャッシュ展開された場合
exePath = fileparts(system_dependent('getexecname'));
else
exePath = fileparts(mfilename('fullpath'));
end
■背景を変える
全体の場合
app.UITable.BackgroundColor = [1 1 1];
特定のセルの場合
addStyle(app.UITable,app.CellStyles{1,3},'cell',[1 3]);
■EXEファイル自身が置かれているフォルダを取得
function exeFolder = getExeFolder()
if isdeployed
exePath = char(java.lang.System.getProperty('sun.java.command'));
exePath = strtrim(exePath);
% 引数付きの場合に備えて先頭の実行ファイルだけ取得
exePath = regexp(exePath, '^"([^"]+\.exe)"|^(\S+\.exe)', ...
'tokens', 'once');
exePath = exePath(~cellfun(@isempty,exePath));
exePath = exePath{1};
exeFolder = fileparts(exePath);
else
exeFolder = fileparts(mfilename('fullpath'));
end
end
■表示の同期
drawnow expose
■Excel読み込み後のcellをNaN→空白に変換
[~,~,C] = xlsread('sample.xlsx');
idx = cellfun(@(x) isnumeric(x) && isscalar(x) && isnan(x), C);
C(idx) = {[]};