4957

Saving/appending output values of varying vector length generated from for loop into separate files

Question:

I need help in iteratively saving or appending the output obtained in terms of "dist" and "P" generated through a function inside a for loop. Although, the output is displayed I am unable to save the various output values from "dist" and "P" in separate files without overwriting.

The input data is provide in two excel files

Test1.xlsx 26 50 52 56 58 59 77 78 79 80 Test2.xlsx 51 52 187 188 189 191 226 227 228 229

and the adjacency matrix I used is adj.xlsx

The matlab code I was using is as below:

adj=xlsread('adjmat.xlsx','Sheet1','A1:JO275') SP1=xlsread('Test1.xlsx','Sheet1','A1:A10'); INHBP=xlsread('Test2.xlsx','Sheet1','A1:A10'); for i=1:length(SP1) for j=1:length(INHBP) [dist,P]=dijkstra(adj,SP1(i),INHBP(j)) end end

The sample output displayed in the matlab workspace is a below:

dist = 27 P = 26 38 40 50 51 dist = 27 P = 26 38 40 50 52 dist = 78 P = 26 38 40 50 52 55 60 63 80 82 116 117 119 187 dist = 88 P = 26 38 40 50 52 55 60 63 80 82 116 117 119 187 188 dist = 98 P = Columns 1 through 15 26 38 40 50 52 55 60 63 80 82 116 117 119 187 188 Column 16 189 dist = 73 P = 26 38 40 70 71 75 76 108 111 113 136 175 178 180 191 dist = 85 P = 26 38 40 50 52 55 60 63 80 82 116 117 118 198 226 dist = 44 P = 26 38 40 50 52 55 60 63 83 227 dist = 33 P = 26 38 40 50 52 55 60 63 166 228

But I want the results to be save in separate files for dist and P (results are varying length of vectors), for instance

dist = 27 27 78 88 98 73 85 44 33 P = 26 38 40 50 51 26 38 40 50 52 26 38 40 50 52 55 60 63 80 82 116 117 119 187 26 38 40 50 52 55 60 63 80 82 116 117 119 187 188 26 38 40 50 52 55 60 63 80 82 116 117 119 187 188 189 26 38 40 70 71 75 76 108 111 113 136 175 178 180 191 26 38 40 50 52 55 60 63 80 82 116 117 118 198 226 26 38 40 50 52 55 60 63 83 227 26 38 40 50 52 55 60 63 166 228

Please help me resolve this!!

Thank in advance.

Ashalatha Sreshty Molecular Biophysics Unit Indian Institute of Science

Answer1:

You can use fopen to create your txt object and then write in this text file with fprintf:

fid1 = fopen('dist.txt','w'); fid2 = fopen('P.txt','w'); adj=xlsread('adjmat.xlsx','Sheet1','A1:JO275') SP1=xlsread('Test1.xlsx','Sheet1','A1:A10'); INHBP=xlsread('Test2.xlsx','Sheet1','A1:A10'); for i=1:length(SP1) for j=1:length(INHBP) [dist,P]=dijkstra(adj,SP1(i),INHBP(j)) b = sprintf('%d ',P); fprintf(fid1,'%d\n',dist) fprintf(fid2,'%s\n',b) end end fclose(fid1); fclose(fid2);

Answer2:

One method would be to dynamically generate a formatspec for <a href="http://www.mathworks.com/help/matlab/ref/fprintf.html" rel="nofollow">fprintf</a> in each of your calculation iterations based on the length of your P vector.

Consider the following example:

dfID = fopen('dist.txt', 'w'); % Discards existing contents pfID = fopen('p.txt', 'w'); % Discards existing contents l = [4 7 2]; % Dummy variable to define length of our P output per loop iteration for ii = 1:length(l) dist = ii; fprintf(dfID, '%u\n', dist); P = ones(1, l(ii)); fprintf(pfID, strcat(strtrim(repmat('%u ', 1, length(P))), '\n'), P); end % Clean up fclose(dfID); fclose(pfID);

Which produces dist.txt:

1 2 3

Along with p.txt:

1 1 1 1 1 1 1 1 1 1 1 1 1

Recommend

  • Saving/appending output values of varying vector length generated from for loop into separate files
  • Implementing a graph in either C++ or Java [closed]
  • Get bad result for random walk
  • using Dijkstra algorithm to find shortest path in an adjacency matrix
  • Data structure for directed graphs, allowing fast node deletion?
  • What is the simplest way to export Excel data to Matlab?
  • Database for efficient large-scale graph traversal
  • How to store a numpy arrays in a column of a Pandas dataframe?
  • Matlab : Storing large numbers
  • Combining pairs in a string (Matlab)
  • Understanding perf detail when comparing two different implementations of a BFS algorithm
  • mySQL transitive closure table
  • In R, is there a similar function to heatmap() to visualize groups in a large matrix?
  • MySQL - Is it possible to get all sub-items in a hierarchy?
  • How to use Neo4j for finding Minimum Spanning Tree?
  • Hadoop Maven Dependency Error
  • Order of columns and rows with ggplot2 tile
  • Modularity calculation for weighted graphs in igraph
  • Bellman-Ford Distance Vector Algorithm With Arbitrarily Many Nodes
  • Creating new ASP.Net MVC project - Unit Test Dialog does not show up
  • Silverlight Tools for Visual Studio 2008 - Placing a Control Issue
  • How can I read in multiple excel files in SPSS using a macro?
  • Reduce size of self-loops in igraph
  • Formula in Excel that references another Excel file based on cell reference
  • Excel Marcro - delete a row based on row number [closed]
  • Excel Range in C# using interop.Excel. Range both empty and not empty?
  • Pop up charts in VBA Excel
  • Find code that depends on .NET 3.5 SP1
  • How do I pass worksheet and ranges as variables?
  • OleDbDataAdapter not filling all of the rows
  • Floating-point number in Excel
  • Visual Basic 6 on Win7 64bit: Will developed program run on 32-bit Win OS?
  • Lua string.gsub() by '%s' or '\\n' pattern
  • Excel distinct count with conditions
  • Not able to display correct data in table -AngularJS
  • several dataProvider per one Test in TestNG
  • quiver not drawing arrows just lots of blue, matlab
  • VBA Convert delimiter text file to Excel
  • How do I use the BLAS library provided by MATLAB?