From f7abd92a7313350c77c16e089f65737c2805fe67 Mon Sep 17 00:00:00 2001 From: sakul-45 <81963567+sakul-45@users.noreply.github.com> Date: Sat, 24 Jul 2021 16:53:06 +0200 Subject: [PATCH] Function not able to get nested structs --- write_params_txt.m | 118 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 114 insertions(+), 4 deletions(-) diff --git a/write_params_txt.m b/write_params_txt.m index 3a9fb2c..415b7c8 100644 --- a/write_params_txt.m +++ b/write_params_txt.m @@ -1,7 +1,117 @@ -function [outputArg1,outputArg2] = write_params_txt(inputArg1,inputArg2) +function [] = write_params_txt(params) %WRITE_PARAMS_TXT Summary of this function goes here % Detailed explanation goes here -outputArg1 = inputArg1; -outputArg2 = inputArg2; -end +filename = strcat(params.Path,"/",params.Name,".txt"); +struct2File(params,filename); + + function struct2File(s,fileName,varargin) + %Write struct to text file. The data in the struct can be both numbers and + %strings. The first line in the file will be a row with the headers/names + %of the columns. + %Ex: struct2File( s, 'c:/test.txt' ); + %Ex: struct2File( s, 'c:/test.txt', 'precision', 3 ); %specify precision + %Ex: struct2File( s, 'c:/test.txt', 'promptOverWrite', false ); + [varargin,align]=getarg(varargin,'align',false); + align=align{:}; + [varargin,delimiter]=getarg(varargin,'delimiter','\t'); + delimiter=delimiter{:}; + [varargin,units]=getarg(varargin,'units',''); + units=units{:}; + [varargin,promptOverWrite]=getarg(varargin,'promptOverWrite',false); + promptOverWrite=promptOverWrite{:}; + [varargin,precision]=getarg(varargin,'precision',6); + precision=precision{:}; + [varargin,Sort]=getarg(varargin,'sort',true); + Sort=Sort{:}; + if ~isempty(varargin) + error('Unknown optional arguments specified'); + end + fields = fieldnames(s)'; + if ~isempty(units) + if numel(units)~=numel(fields) + error('The number of units specified doesn not match the number of fields in the struct'); + end + end + if exist(fileName,'file')==2 && promptOverWrite + res = questdlg('File exists, overwrite?','', ... + 'Yes', 'No', 'Yes'); + if strcmpi(res,'No') + disp('Aborted'); + return; + end + end + data=cell(numel(s),numel(fields)); + for k=1:numel(fields) + fn=fields{k}; + data(:,k) = {s.(fn)}; + end + if size(units,2)==1 + units = units'; + end + if ~isempty(units) + data=[units;data]; + end + data=[fields;data]; + if Sort + [fields,ind] = sort(fields); + data = data(:,ind); + end + %ex1 = {'a' 1 12 123; 'ab' 4 5 6; 'abc' 7 8 9}; + ex_func3 = @(input)ex_func(input,precision); + ex2 = cellfun(ex_func3,data,'UniformOutput',0); + if align + size_ex2 = cellfun(@length,ex2,'UniformOutput',0); + str_length = max(max(cell2mat(size_ex2)))+1; + ex2 = cellfun(@(x) ex_func2(x,str_length),ex2,'uniformoutput',0); + ex2 = cell2mat(ex2); + end + fid = fopen(fileName,'wt'); + if fid==-1 + error('Could not open %s'); + end + if iscell(ex2) + [m,n]=size(ex2); + for i=1:m + for j=1:n + fprintf(fid,'%s',ex2{i,j}); + if j