compile script to create program

Hi
I've made made a script that is able to sort my "list2.csv" and then put it into a table. This list2.csv was only the test list. Now i need to change my script so it is possible to use it as a function on any list(with same amount of columns) that is put into it. can You guys please help me.
THX!!

7 Commenti

Jan
Jan il 10 Apr 2017
All you need is to use a variable instead of a ficed file name. But without seeing the code it is impossible to suggest explicite changes.
filename = (List);% 'C:\Users\PET-Center\Documents\MATLAB\KFList.csv';
delimiter = ',';
startRow = 2;
%%Read columns of data as strings:
% For more information, see the TEXTSCAN documentation.
formatSpec = '%q%q%q%q%q%q%q%q%q%q%q%[^\n\r]';
%%Open the text file.
fileID = fopen(filename,'r','n','UTF-8');
% Skip the BOM (Byte Order Mark).
fseek(fileID, 3, 'bof');
%%Read columns of data according to format string.
% This call is based on the structure of the file used to generate this
% code. If an error occurs for a different file, try regenerating the code
% from the Import Tool.
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'HeaderLines' ,startRow-1, 'ReturnOnError', false);
%%Close the text file.
fclose(fileID);
%%Convert the contents of columns containing numeric strings to numbers.
% Replace non-numeric strings with NaN.
raw = repmat({''},length(dataArray{1}),length(dataArray)-1);
for col=1:length(dataArray)-1
raw(1:length(dataArray{col}),col) = dataArray{col};
end
numericData = NaN(size(dataArray{1},1),size(dataArray,2));
%%Split data into numeric and cell columns.
rawNumericColumns = {};
rawCellColumns = raw(:, [1,2,3,4,5,6,7,8,9,10,11]);
%%Create output variable
KFList = raw;
%%Clear temporary variables
clearvars filename delimiter startRow formatSpec fileID dataArray ans raw col numericData rawNumericColumns rawCellColumns;
%%%%Initialize variables.
filename = 'C:\Users\PET-Center\Documents\MATLAB\key2.csv';
delimiter = ',';
%%Format string for each line of text:
% column1: text (%q)
% column2: double (%f)
% For more information, see the TEXTSCAN documentation.
formatSpec = '%q%f%[^\n\r]';
%%Open the text file.
fileID = fopen(filename,'r');
%%Read columns of data according to format string.
% This call is based on the structure of the file used to generate this
% code. If an error occurs for a different file, try regenerating the code
% from the Import Tool.
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'ReturnOnError', false);
%%Close the text file.
fclose(fileID);
%%Post processing for unimportable data.
% No unimportable data rules were applied during the import, so no post
% processing code is included. To generate code which works for
% unimportable data, select unimportable cells in a file and regenerate the
% script.
%%Create output variable
dataArray(2) = cellfun(@(x) num2cell(x), dataArray(2), 'UniformOutput', false);
key2 = [dataArray{1:end-1}];
%%Clear temporary variables
clearvars filename delimiter formatSpec fileID dataArray ans;
%%NoOfLabels
NewOutput=cell(0);
for n = 1:size(KFList,1);
if strcmp(KFList(n,11), 'not specified');
KFList(n,11)=KFList(n,9);
end
for t=1:size(key2,1);
tf = strcmp(KFList(n,11),key2(t));
if tf == 1;
Tcells=repmat(KFList(n,:),[key2{t,2} 1]);
NewOutput(size(NewOutput,1)+1:size(NewOutput,1)+key2{t,2},:)=Tcells;
end
end
end
% KeyTable
%%Sorted by Batch Discription
[BD,Index]=sort(NewOutput(:,11));
BatchDescription = BD(Index);
%[RN,Index] = sort(NewOutput(:,)); %RoomNumber
%RoomNumber = RN(Index);
%RD = NewOutput(:,);
%ReferenceDepartment = RD(Index); %ReferenceDepartment
PID= NewOutput(:,1); % Medicalrecordnumber
PatientID = PID(Index);
ST = NewOutput(:,2); % Statusname
StatusName = ST(Index);
AP = NewOutput(:,3); % Appointmentdatetime
AppointmentDateTime = AP(Index);
DC = NewOutput(:,4); %Departmentcode
DepartmentCode = DC(Index);
FN = NewOutput(:,5); %Fullname
FullName = FN(Index);(Index);
CI = NewOutput(:,6); %Clinicalinfo
ClinicalInfo = CI(Index);
PPCode = NewOutput(:,7); %Procedureprotocolcode
ProcedureProtocolCode = PPCode(Index);
PPName = NewOutput(:,8); %Procedureprotocolname
ProcedureProtocolName = PPName(Index);
PN = NewOutput(:,9); %Procedurename
ProcedureName = PN(Index);
PC = NewOutput(:,10); %Procedurecode
ProcedureCode = PC(Index);
SortedList2 = [PatientID, FullName,BatchDescription, AppointmentDateTime, ProcedureProtocolName]; %StatusName, ClinicalInfo, DepartmentCode, ProcedureProtocolCode, , ProcedureCode,ProcedureName];
KeyTable = cell2table(SortedList2,'VariableNames',{'PatientID','PatientName','BatchDiscription', 'Date', 'ProcedureProtocolName'}); % 'Statusname','Department code',...
%'ProcedureProtocolCode','Procedure name 'Procedure Code'});
writetable(KeyTable,'TableTest.csv','Delimiter',',','QuoteStrings',true);
type 'TableTest.csv';
if true
% code
end
Just put
function myFunction( filename )
at the top and remove the first line, then call it as e.g.
myFunction( 'C:\Users\PET-Center\Documents\MATLAB\KFList.csv' )
though call it something more sensible - it needs to be the same as your file name in which the code sits.
Anne
Anne il 10 Apr 2017
thank you! =)
I was a little too fast. I get an error when I change the second line to
myFunction('C:\Users\PET-Center\Documents\MATLAB\KFList.csv');
function myFunction(filename) ↑ Error: Function definitions are not permitted in this context.
Adam
Adam il 10 Apr 2017
Don't change the second line, put that (though again, use a sensible name) as the first line and remove the current first line. The function signature has to be the first thing in the file.
now i've tried to write
function Listfunc(filename)
And then I called it like
Listfunc('C:\Users\PET-Center\Documents\MATLAB\KFList.csv')
I've godt the same error message again

Accedi per commentare.

Risposte (1)

Stephen23
Stephen23 il 10 Apr 2017
Modificato: Stephen23 il 10 Apr 2017
function myfun(filename)
%
delimiter = ',';
startRow = 2;
... the rest of your code.
end
And then call it using:
myfun( 'C:\Users\PET-Center\Documents\MATLAB\KFList.csv' )

Richiesto:

il 10 Apr 2017

Commentato:

il 11 Apr 2017

Community Treasure Hunt

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

Start Hunting!

Translated by