LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Write to txt file with constant tab size

Solved!
Go to solution

Hi,

 

I want to store my data into a .txt file. I did this as shown in the attached image.

However, the data does not have a constant size. By using the \t delimiters I get sometimes the right column representation in my text file:

Force          Pressure 1          Pressure 2

100.3          285.8                   503.29

 

But sometimes I get:

Force          Pressure 1          Pressure 2

1000032.6                82.5                 78.236

 

I was wondering if it is possible to set a constant tab size to get a correct column representation.

 

Note, I know that with TDMS files this is possible. However, I highly prefer a .txt file because the data will be processed in Matlab. And loading large TDMS files (which I am supposed to do) in Matlab is horrible.

 

Thanks in advance.

 

 

 

0 Kudos
Message 1 of 12
(6,020 Views)
Solution
Accepted by topic author Bryan_1995

This is not a LabVIEW issues, but an OS issue.  The OS defines how many "spaces" a tab takes up upon display.  Heck that may even vary from application to application.  It is certainly different between Notepad and Word (where the distance between tab stops is a formatting property.)

 

I think you are confusing appearances with data representation.  In your shown example, your appearance is based on the HTML formatting of the NI website.  The problem is that if your title is dramatically different in length from your data element, enough that one or the other reaches beyond the arbitrary tab stop of whatever program you are using the data, it is never going to appear the way you want.

 

You say you are doing this in Matlab.  If it is to programmatically read the file and analyze, don't worry about appearance.  Doing anything will just complicate the parsing of the file in to data.

 

If it is just to display the data in some text window and you don't care about extra characters screwing things up, first you need to make sure that text window is set up some monospaced font like Courier.  You could send the data out with string formatting codes that are fixed some over places.  So a format code of %-16s would take any string up to 16 characters.  Any missing characters would be filled with spaces.  So "Force" would be followed by 11 spaces.

Message 2 of 12
(6,008 Views)

@RavensFan wrote:

This is not a LabVIEW issues, but an OS issue.


It's not really an OS issue, but a text editor issue.  In Word, UltraEdit, etc, I can set the tab size.  I don't think Notepad allows you to.  So if there is an option to set the tab size, just make it bigger.

 

Alternatively, just open the text file in Excel.  Excel will read a tab delimited text file with no issues and the columns will be properly filled in.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 3 of 12
(5,992 Views)
Solution
Accepted by topic author Bryan_1995

Change your file extension from .txt to .tdl. That will let Excel open the file and will show the data properly formatted, and Matlab can still read it in as tab-delimited.

 

(tdl = tab delimited file, like csv = comma separated values)

Message 4 of 12
(5,976 Views)

@BertMcMahan wrote:

Change your file extension from .txt to .tdl.


I have never had to do that.  Excel opens up txt files for me with no issues.  I do it all the time.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 5 of 12
(5,949 Views)

Thank you for your fast reply.

 


@RavensFan wrote:

So a format code of %-16s would take any string up to 16 characters.  Any missing characters would be filled with spaces.  So "Force" would be followed by 11 spaces.


 

I implemented this. I know the maximum possible digits for each parameter. Using this method I am able to reserve for every parameter the maximum needed space.

 

Thanks!

0 Kudos
Message 6 of 12
(5,942 Views)

@BertMcMahan, Thanks for your reply,

 

I implemented this and it works indeed, so it is definitely a possible solution to the problem.

However, I like the constant tab space as suggested by slightly more.

 

Thank you for your suggestion!

0 Kudos
Message 7 of 12
(5,940 Views)

@crossrulz wrote:

@BertMcMahan wrote:

Change your file extension from .txt to .tdl.


I have never had to do that.  Excel opens up txt files for me with no issues.  I do it all the time.


Same here, but Excel will open automatically with a .tdl file extension. .txt opens in Notepad, and you have to manually right click->open with -> Excel (or open Excel, then File-Open, etc).

 

In my experience it's much more likely to want to read tab-delimited data with Excel than a text editor, but it's no hard and fast rule.

0 Kudos
Message 8 of 12
(5,920 Views)

@BertMcMahan wrote:

Same here, but Excel will open automatically with a .tdl file extension.


I'm not having that experience.  I just changed one of my data files to a tdl extension and now Windows has no clue what to do with the file.  So I'm guessing you configured Windows to open a tdl file with Excel.

 

I typically just open Excel and then drag my files over if I really need to do something with them in Excel.  Otherwise, I have Ultra Edit set the tabs fairly large so my columns usually line up.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 9 of 12
(5,914 Views)

@crossrulz wrote:

@BertMcMahan wrote:

Same here, but Excel will open automatically with a .tdl file extension.


I'm not having that experience.  I just changed one of my data files to a tdl extension and now Windows has no clue what to do with the file.  So I'm guessing you configured Windows to open a tdl file with Excel.

 

I typically just open Excel and then drag my files over if I really need to do something with them in Excel.  Otherwise, I have Ultra Edit set the tabs fairly large so my columns usually line up.


Well it looks like I owe the forum an apology, you're right.🤐 Thanks for letting me know. My Excel does recognize *.tdl files but I can't for the life of me remember setting that up. I must be getting mixed up with csv, which I think Excel will open natively (but who knows, I could've made that setting years ago and forgotten too).

 

It's funny because I basically never use the tdl file extension, so I wouldn't have a reason to set it up to open in Excel automatically. We use .csv for delimited files around here.

 

Anyway, to the OP and anyone else, this is apparently a setting on my machine that I made several years ago and forgot. I can't go back and edit my above post anymore so hopefully it doesn't confuse anyone else.

 

It *is* a fairly convenient setting though 🙂

0 Kudos
Message 10 of 12
(5,905 Views)