From 261b9a83b081b013128cb8a4ade4d179d9415a26 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?William=20L=C3=B6vfors?= <william.lovfors@liu.se>
Date: Mon, 3 May 2021 13:57:37 +0200
Subject: [PATCH] updated structfieldcat to handle 1D arrays, and
 FindBestParametersFile to handle one given parameter file

---
 FindBestParametersFile.m | 25 +++++++++++++++----------
 StructFieldCat.m         |  9 +++++++--
 2 files changed, 22 insertions(+), 12 deletions(-)

diff --git a/FindBestParametersFile.m b/FindBestParametersFile.m
index 5e15d89..7766358 100644
--- a/FindBestParametersFile.m
+++ b/FindBestParametersFile.m
@@ -13,15 +13,20 @@ end
 files=dir(pathToSearch);
 assert(length(files)>0,'No files to search in the provided path')
 names=string({files.name});
-files(cellfun(@isempty,regexp(names, key)))=[]; %removes all files not containing "key"
-
-names=string({files.name});
-values=double(regexprep(names,{'.*\(','\).*'},{'',''}));
-assert(any(~isnan(values)),'No files contains valid names. Make sure the files has the cost in parenthesis, e.g opt(12.34)');
-[~,bestInd]=min(values);
-name=files(bestInd).name;
-path=[files(bestInd).folder '/' name];
-dirStruct=files(bestInd);
-
+if length(files)>1
+    files(cellfun(@isempty,regexp(names, key)))=[]; %removes all files not containing "key"
+    
+    names=string({files.name});
+    values=double(regexprep(names,{'.*\(','\).*'},{'',''}));
+    assert(any(~isnan(values)),'No files contains valid names. Make sure the files has the cost in parenthesis, e.g opt(12.34)');
+    [~,bestInd]=min(values);
+    name=files(bestInd).name;
+    path=[files(bestInd).folder '/' name];
+    dirStruct=files(bestInd);
+else
+    assert(contains(names,key), "File does not contain provided key")
+    path =[files.folder '/' files.name];
+    dirStruct=files;
+end
 end
 
diff --git a/StructFieldCat.m b/StructFieldCat.m
index a2ba43e..9657ebc 100644
--- a/StructFieldCat.m
+++ b/StructFieldCat.m
@@ -6,7 +6,12 @@ for i=1:size(T,2)
     fields = fieldnames(S);
     for k = 1:numel(fields)
         aField     = fields{k}; % EDIT: changed to {}
-        
-        S.(aField) = cat(dim, S.(aField), T(i).(aField));
+        if size(S.(aField),1) ==1 && size(S.(aField),2)>1 % If field is a horizontal 1D array
+            S.(aField) = horzcat(S.(aField), T(i).(aField));
+        elseif size(S.(aField),2) > 1 && size(S.(aField),1)>1 %if field is a vertical 1D array
+            S.(aField) = vertcat(S.(aField), T(i).(aField));
+        else
+            S.(aField) = cat(dim, S.(aField), T(i).(aField));
+        end
     end
 end
\ No newline at end of file
-- 
GitLab