473,382 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How to write a matrix or array to a text file?

Hello,

i want to write an array to a text file using visual basic. the output should look like this for a 5X5 sized array.

0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 1 0
0 0 0 0 0

All the elements will be zero except one. When i am trying to write using Print#1 statement then i am getting one element on one line. How to do this???

thank you in advance
Aug 27 '07 #1
11 8627
Killer42
8,435 Expert 8TB
Does it actually matter what the data looks like in the file, or do you just need to be able to read it back into the array?
Aug 27 '07 #2
Does it actually matter what the data looks like in the file, or do you just need to be able to read it back into the array?

hello,

the data should look like as typed by me, as it will be read as array (a matrix of 5 X 5) afterwords. so its necessary to have spaces in between & 5 items in a row.
The matrix size will change according to the requirements, so its not possible to use a statement like- Print #1 0 0 0 0 0.

Thank you for your response.
Aug 27 '07 #3
Killer42
8,435 Expert 8TB
The point I was trying to make is that you can store an array straight to file in one statement, and retrieve it again later.

Otherwise, I'd say what you need to know is that you can end the Print statement with a semicolon (;) to leave out the line break. For example:

Expand|Select|Wrap|Line Numbers
  1. Print #1, MyArray(1);
This will not insert a line break, so the next thing you write/print to the file will continue on the same line.
Aug 27 '07 #4
hariharanmca
1,977 1GB
hello,

the data should look like as typed by me, as it will be read as array (a matrix of 5 X 5) afterwords. so its necessary to have spaces in between & 5 items in a row.
The matrix size will change according to the requirements, so its not possible to use a statement like- Print #1 0 0 0 0 0.

Thank you for your response.
Just try like this.....

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Dim xyz(5, 5) As String
  3. Dim tmpStrValue As String
  4. Dim i As Integer
  5. Dim j As Integer
  6.  
  7. Private Sub PrintMatrixForum()
  8.     For i = 1 To 5
  9.         For j = 1 To 5
  10.             xyz(i, j) = i + j
  11.         Next j
  12.     Next i
  13.     tmpStrValue = ""
  14.     For i = 1 To 5
  15.         For j = 1 To 5
  16.             tmpStrValue = tmpStrValue & xyz(i, j) & " "
  17.         Next j
  18.         tmpStrValue = tmpStrValue & vbCrLf
  19.     Next i
  20.     MsgBox tmpStrValue
  21.     tmpStrValue = ""
  22. End Sub
Aug 27 '07 #5
The point I was trying to make is that you can store an array straight to file in one statement, and retrieve it again later.

Otherwise, I'd say what you need to know is that you can end the Print statement with a semicolon (;) to leave out the line break. For example:

Expand|Select|Wrap|Line Numbers
  1. Print #1, MyArray(1);
This will not insert a line break, so the next thing you write/print to the file will continue on the same line.
hi,

I dont have any array with me i have to create it, may be using for loop with i & j. I will try out putting semicolon at the end of statement to see whether it works or not.

thank you
Aug 27 '07 #6
hariharanmca
1,977 1GB
hi,

I dont have any array with me i have to create it, may be using for loop with i & j. I will try out putting semicolon at the end of statement to see whether it works or not.

thank you

is it you front-end VB, then why to have semicolon at the end?
Aug 27 '07 #7
The point I was trying to make is that you can store an array straight to file in one statement, and retrieve it again later.

Otherwise, I'd say what you need to know is that you can end the Print statement with a semicolon (;) to leave out the line break. For example:

Expand|Select|Wrap|Line Numbers
  1. Print #1, MyArray(1);
This will not insert a line break, so the next thing you write/print to the file will continue on the same line.
Its working now,

thanks a lot Killer
Aug 27 '07 #8
is it you front-end VB, then why to have semicolon at the end?

great!!!!!!!!!

I have solved the problem with help of you and killer.

thanks to both of you.

Regards
Aug 27 '07 #9
Killer42
8,435 Expert 8TB
is it you front-end VB, then why to have semicolon at the end?
That was my suggestion - a semicolon on the end of the Print # statement to skip the line break, so the next element is written on the same line. Very much like your code, except that I assumed that deeptismuley wanted to write the elements individually to the file, while you are accumulating them into a string, which can then be written.

Just a slightly different approach, is all.
Aug 27 '07 #10
hariharanmca
1,977 1GB
That was my suggestion - a semicolon on the end of the Print # statement to skip the line break, so the next element is written on the same line. Very much like your code, except that I assumed that deeptismuley wanted to write the elements individually to the file, while you are accumulating them into a string, which can then be written.

Just a slightly different approach, is all.

For that we have to use ' : '
But i never tried ' ; '
Aug 27 '07 #11
Killer42
8,435 Expert 8TB
For that we have to use ' : '
But i never tried ' ; '
Perhaps there are differences in versions of VB from around the world. But in my version, the colon (:) is used to delimit multiple statements on a line, and the semicolon (;) is used between elements in a Print statement to concatenated them.

Examples...
Expand|Select|Wrap|Line Numbers
  1. A = 1 : B = 2 '       <-- Colon between statements
  2. Debug.Print A; B '    <-- Semicolon between printed elements.
Aug 27 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Tony Johansson | last post by:
Hello Experts!! I have two small classes called Intvektor and Matris shown at the bottom and a main. Class Intvektor will create a one dimension array of integer by allocate memory dynamically...
6
by: memocan | last post by:
#include <iostream> using namespace std; int x; //global variable matrix int main() { x= new float ; //initialize the size now }
16
by: Martin Jørgensen | last post by:
Hi, I've made a program from numerical recipes. Looks like I'm not allowed to distribute the source code from numerical recipes but it shouldn't even be necessary to do that. My problem is...
4
by: pariub | last post by:
So here is my problem once... i have a file called network.txt ...its a 13X13 matrix...but the network.txt can go up to 100X 100. I should read the text file and store it in array . ...
20
by: Frank-O | last post by:
Hi , Recently I have been commited to the task of "translating" some complex statistical algorithms from Matlab to C++. The goal is to be three times as fast as matlab ( the latest) . I've...
8
by: mohammaditraders | last post by:
#include <iostream.h> #include <stdlib.h> #include <conio.h> #include <string.h> class Matrix { private : int numRows, numCols ; int elements ;
2
by: Marco Biagioni | last post by:
After i've tried to update a vb 6.0 project to vb.net, using visual studio utility,i can't read correctly data bytes from a .bmp file to insert them in a matrix to operate on. Using vb 6.0 the...
2
by: Marco Biagioni | last post by:
After i've tried to update a vb 6.0 project to vb.net, using visual studio utility,i can't read correctly data bytes from a .bmp file to insert them in a matrix to operate on. Using vb 6.0 the...
1
by: csgirlie | last post by:
I'm trying to store or arrange three sets of two-dimensional data into three 2xN matrices that are stored as NumPy arrays. import os # for file handling functions import numpy as...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.